Search code examples
c#asp.net-web-apihttp-status-code-404httpresponse

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


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?


Solution

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

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