0

I'm working with ASP.Net MVC 5 and Web Api 2 Controller and I'm facing a really annoying thing.

When I send an HttpResponseMessage using, for example, a 400 status code, I would like to send a string message with the reason.

If I return an HTTP 200 the content is displayed correctly, but if I do something like this:

HttpResponseMessage response = new HttpResponseMessage();
response.Content = new StringContent("This is a bad request man.");
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/text");
response.StatusCode = HttpStatusCode.BadRequest;
return response; 

It doesn't seem to work.

Is this a bad practice? Am I wrong when I think that I should return a message in this case?

Thanks in advance!

Nkosi
  • 235,767
  • 35
  • 427
  • 472
magicleon94
  • 4,887
  • 2
  • 24
  • 53
  • 1
    Repeated question https://stackoverflow.com/questions/10732644/best-practice-to-return-errors-in-asp-net-web-api – Harsh Chaurasia Jan 22 '19 at 09:53
  • I believe it's not repeated. Maybe the part on the best practices is, but I'd like to know why the thing I wrote doesn't work – magicleon94 Jan 22 '19 at 09:57

0 Answers0