How do I return NotFound() IHttpActionResult with an error message or exception?

Ajay Jadhav picture Ajay Jadhav · Nov 22, 2013 · Viewed 85.2k times · Source

I am returning a NotFound IHttpActionResult, when something is not found in my WebApi GET action. Along with this response, I want to send a custom message and/or the exception message (if any). The current ApiController's NotFound() method does not provide an overload to pass a message.

Is there any way of doing this? or I will have to write my own custom IHttpActionResult?

Answer

Anthony F picture Anthony F · Jun 19, 2015

Here's a one-liner for returning a IHttpActionResult NotFound with a simple message:

return Content(HttpStatusCode.NotFound, "Foo does not exist.");