Why does HttpException.GetHttpCode () return an int instead of System.Net.HttpStatusCode?

I know that I can just cast the result of GetHttpCode () to HttpStatusCode , but I don't like the type conversion. Moreover, the MSDN document does not explicitly state that enumeration values ​​are always the exact corresponding http status code.

I feel that GetHttpCode () should just return an HttpStatusCode .

Should I just stop complaining and come up with?

catch(HttpException ex) { switch((HttpStatusCode)ex.GetHttpCode()) { case HttpStatusCode.NotFound: // Do stuff... break; // More cases... } } 

EDIT: note that HttpWebResponse.StatusCode is of type HttpStatusCode

+4
source share
5 answers

It is worth noting that HTTP allows custom status codes, so it does not guarantee that the enumeration will contain the value from your response. For this reason, the API must return an int value instead of trying to use an enumeration.

+8
source

HTTP status codes do not have to come from a fixed list, although this is usually done. Using an enumeration is a convenience. A listing requirement would be incorrect.

+1
source

The response contains an integer value for the error code. Int is the actual error code. Enumeration allows a "friendly" representation of the error code.

0
source

Should I just stop complaining and come up with?

Yes. Or work with the appropriate integer values.

0
source

Yes, you have to do it like this. First of all, since the code that you yourself provided protects you from working with any integer values ​​or state numbers. So technically it doesn’t matter if the enum values ​​match the status number values.

0
source

Source: https://habr.com/ru/post/1437186/


All Articles