2

I am creating a .net core api. For now it works well, but I would like to provide in the answers more useful information so that the customer can directly consume said information and not find a correct way to do it.

I have a generic driver that returns for now a vector with three objects with the information of the genres that are in the database:

[HttpGet]
        public IEnumerable<Gender> GetAll()
        {
            List<Gender> GenderModel = new List<Gender>();
            genderService.GetGender().ToList().ForEach(u => {
                Gender genero = new Gender
                (
                 u.Id,
                 u.Name
                );
                GenderModel.Add(gender);
            });

            return GenderModel.ToList();
        }

The answer is as follows:

[
  {
    "id": 1,
    "value": "Masculino"
  },
  {
    "id": 2,
    "value": "Femenino"
  },
  {
    "id": 3,
    "value": "Otros"
  }
]

however, I would like the answer to have this form:

{
    "results": [
        {
            "id": 0,
            "value: "Otro"
        },
        {
            "id": 1,
            "value": "Masculino"
        },
        {
            "id": 2,
            "value": "Femenino"
        }
    ],
    "info": {
        "results": 3,
        "version": "1.0"
    }
}

Obviously, any other information you want to add either as another JSON object or within the info object would also have to be sent. My problem is how to compose that answer. Another thing that is not quite clear is, the response from the .net core webapi is in vector format with objects inside, and usually all the apis I worked with were a response in JSON with other JSON objects inside , I have no problem working with this format in the front-end but I came to doubt.

Ata Sanchez
  • 511
  • 3
  • 16

2 Answers2

2

Create POCO class to model api responses:

        public class ApiResponse{
            public object Results { get; set; }
            public object Info { get; set; }
        }


        //Implementation of ApiResponse
        [HttpGet]
        public ApiResponse GetAll()
        {
                var data = genderService.GetGender().Select(g => 
                 new Gender(){
                   Id = g.Id,
                   Name = g.Name
                 }).ToList();

                var apiResponse = new ApiResponse();
                apiResponse.Results = data;
                apiResponse.Info = new {Results = data.Length, Version = "1.0"};

                return apiResponse;
        }
Yared
  • 1,817
  • 1
  • 19
  • 28
2

You should change returning object. ActionResult pack you response with ASP.net core return Json with status code

[HttpPost]        
    public new async Task<IActionResult> Get([FromBody] QueryParams parameters)
    {
            List<Gender> GenderModel = new List<Gender>();
            genderService.GetGender().ToList().ForEach(u => {
                Gender genero = new Gender
                (
                 u.Id,
                 u.Name
                );
                GenderModel.Add(gender);
            });

            return Ok(GenderModel.ToList();
    }

Next if your want to provide more details in responses, use ActionFilter in

    public void OnActionExecuted(ActionExecutedContext context)
    {
        // do something after the action executes
        // your reponse is in context.response.content 
    }
Tomasz Żmuda
  • 589
  • 5
  • 8