1

I am looking for, but not finding, a list of all acceptable return options for IHttpActionResult. I know of these two:

  • OK(object)
  • NotFound()

Are there more? And where are they documented?

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193

3 Answers3

3

There are tons:

See: https://msdn.microsoft.com/en-us/library/system.web.http.results(v=vs.118).aspx

This one is my favorite because you can put custom return codes in it e.g.: NoContent 204

https://msdn.microsoft.com/en-us/library/system.web.http.results.statuscoderesult(v=vs.118).aspx

Example:

public async Task<IHttpActionResult> DoIt()
{
     return StatusCode(HttpStatusCode.NoContent);
}

Basically all http error codes are supported, I would advise to stick with them for compliance reasons.

Stefan
  • 17,448
  • 11
  • 60
  • 79
  • marked as answer because of the example. Thanks! – Casey Crookston Aug 23 '17 at 19:06
  • If you want something custom, it's worth checking out this post: https://stackoverflow.com/questions/20139621/how-do-i-return-notfound-ihttpactionresult-with-an-error-message-or-exception – Stefan Aug 23 '17 at 19:12
2

Is this what you're looking for?

https://msdn.microsoft.com/en-us/library/system.net.httpstatuscode(v=vs.118).aspx

lancew
  • 780
  • 4
  • 22
2

All of those methods are implemented by ApiController, the base class for your Web API controllers.

https://msdn.microsoft.com/en-us/library/system.web.http.apicontroller(v=vs.118).aspx

Every method on that page with a description that starts with "Creates a" is such a method.