Turning the HttpResponseMessage return into JSON

0

My API currently returns the bank values in JSON.

If no value is found, I return a HttpResponseMessage with error code 404.

However, when testing the request, only the message contained in Content is displayed.

I would like to return all the filled values ( Content , StatusCode and ReasonPhrase ) in the JSON style (key-value).

Is this possible?

[HttpGet]
[Route("user/")]
public List<user> Get()
{
    new UserController();
    IQueryable<user> users = from itens in db.user select itens;
    if (!users.Any())
    {
        HttpResponseMessage error = new HttpResponseMessage(HttpStatusCode.NotFound)
        {
            Content = new StringContent(string.Format("Nenhum usuário encontrado")),
            StatusCode = HttpStatusCode.NotFound,
            ReasonPhrase = "User Not Found"
            };
            throw new HttpResponseException(error);
        }
        return users.ToList();
    }
}
    
asked by anonymous 14.02.2018 / 18:44

1 answer

2

StatusCode and ReasonPhrase are part of the response header. If you want this to be a part of the body you will have to concatenate the values in Content or customize the answer.

Maybe you should rethink this need a bit, since the response header will always be available to the client (the application that consumes the API) to access.

    
14.02.2018 / 19:10