0

I know that my methods can return:

  • 200 OK - if all goes well;
  • 401 Unauthorized - if unauthorized;
  • 422 Unprocessable Entity - because I explicitly return it in some of the actions;
  • 500 Internal Server Error - in case there's an unexpected exception thrown somewhere.

I'm wondering, if any other status codes are possible if I don't explicitly return them - i.e. if it can happen outside of my actions?

Edit:

It seems like I wasn't clear enough. To make it clearer: I know that I can return any code that I want. The question is, what codes can the framework return without my direct intention (i.e. if something can happen outside of my actions that will return an error code different than 500). Like with 401 Unauthorized - I'm not returning it explicitly, it just 'happens'.

user622505
  • 743
  • 6
  • 23
  • 2
    You can return any code you like, it's just an integer. – DavidG Jul 25 '17 at 23:20
  • @DavidG please see my comment under Xin's answer. – user622505 Jul 25 '17 at 23:47
  • The answer is still potentially any code. For example, there could be a middleware component that looks at the request and if there's no header called `MilkAndTwoSugars` it will return `418 I'm a teapot`. Unlikely of course, and not part of the default "framework", but possible. – DavidG Jul 25 '17 at 23:51
  • What kind of problem you are going to solve? – Sir Rufo Jul 25 '17 at 23:54
  • It also depends on your web server. IIS returns Http 404 if the resource is not found. So if your consumer misspelt your URL, it'd get a 404. (Of course, your app can intercept the 404 in that case and return something else.) – Frank Fajardo Jul 25 '17 at 23:59
  • @SirRufo I'm writing an API that will be used by a mobile app. I'm arguing for using proper status codes, but my coworker developing the app complains that they'd have to handle too many status codes (and the app is less flexible in deployment than the web API is) and instead they want me to return an OK response even in case of errors (with an error string inside of the response object), so that when any error that I didn't anticipate is thrown, they could just display a generic 'An error occurred' message instead of worrying about status codes, and display the actual error message otherwise. – user622505 Jul 26 '17 at 00:11
  • Returning 200 on any request and having the real status in the response object is not so uncommon for a WebApi. f.i. Imgur does so https://api.imgur.com/#responses – Sir Rufo Jul 26 '17 at 00:41
  • I would expect any official status code. You don't have to handle each one; rather, you could just check the first digit (2, 4, 5) and handle accordingly. I agree that the service should return meaningful codes. Returning 200 OK when errors occur is misleading. – Dan Wilson Jul 26 '17 at 02:22

1 Answers1

0

Check this enum HttpStatusCode

Also you can refer to: https://msdn.microsoft.com/en-us/library/system.net.httpstatuscode(v=vs.110).aspx

Xin
  • 33,823
  • 14
  • 84
  • 85
  • Thanks for your answer, but it doesn't answer my question. I realize that I can return any code I like, it doesn't even have to be in the enum. The question is what codes can be returned without my direct and explicit intention. I'll clarify it further in the question. – user622505 Jul 25 '17 at 23:47