Creating new IHttpActionResult action result methods

Intrepid picture Intrepid · Feb 5, 2015 · Viewed 27.9k times · Source

Is there a way I can use the new IHttpActionResult interface to return a HttpStatusCode.NoContent response message?

I am currently using return new HttpResponseMessage( HttpStatusCode.NoContent ); and would like to convert this into return NoContent();.

IHttpActionResult has already got Ok(), Conflict() and NotFound() but I cannot find any for Forbidden() and NoContent() which I need to use in my project.

How easy is it to add other result types?

Answer

Rafael Companhoni picture Rafael Companhoni · Feb 5, 2015

There's no convenience method for no-content result because, by default, when a action returns void, the response will have the HTTP status 204.

If you wish to explicitly indicate that on the action, you could also return a StatusCode(HttpStatusCode.NoContent) from your action or a

ResponseMessage(new HttpResponseMessage(HttpStatusCode.NoContent)).

The Unauthorized() convenience method gives you a 401 status so, for Forbidden (403), you would also have to use StatusCode(HttpStatusCode.Forbidden) or

ResponseMessage(new HttpResponseMessage(HttpStatusCode.Forbidden))