I have this :
[HttpDelete]
public HttpResponseMessage DeleteClient(int idCliente)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
how to return a message text next to status?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Why do you have HttpResponseException in the subject? If you really need to return an error status code with a message while throwing an exception, HttpResponseException has a constructor that takes an HttpResponseMessage instance.
But in .NET Core that exception only in the Microsoft.AspNetCore.Mvc.WebApiCompatShim backwards compatibility package. The recommended way is just to return the HttpResponseMessage with an error status code directly.
Method 2
You could do something like this:
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("The Message")
};
If you want to return JSON (using Newtonsoft.Json library), you could do:
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(
JsonConvert.SerializeObject(new { message = "The Message" }),
Encoding.UTF8, "application/json")
};
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0