0
My API currently returns database values in JSON.
If no value is found, I return one HttpResponseMessage with error code 404.
However, when testing the request, only the message contained in Content is presented.
I would like to return all values filled in (Content, StatusCode and ReasonPhrase) JSON style (key-value).
That’s 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();
}
}
StatusCodeandReasonPhraseare part of the answer header. If you want this to be part of the body you will have to concatenate the values intoContentor personalize the response.– Jéf Bueno
@LINQ thanks for the reply. I saw that it is possible through your reply, but my little experience tells me that maybe it is not the best way. How is this treatment made in production?
– Csorgo
I never made use of the header values in a JSON, because it is always possible to recover these values on the client side. Wouldn’t it be better to just change the rule in the client application?
– Jéf Bueno
We have not yet implemented the receipt of data in the client. The test was conducted via Postman. I believe that with this content I can follow. Thanks for the info. I could edit the comment as a reply?
– Csorgo