问题
HttpStatusCode
is implemented as an enum
, with each possible value assigned to its corresponding HTTP status code (e.g. (int)HttpStatusCode.Ok == 200
).
However, HttpMethod
is implemented as a class, with static properties to get instances for the various HTTP verbs (HttpStatus.GET
, HttpStatus.PUT
etc). What is the rationale behind not implementing HttpMethod
as an enum
?
回答1:
From the documentation (emphasis mine):
Remarks
The most common usage of HttpMethod is to use one of the static properties on this class. However, if an app needs a different value for the HTTP method, the HttpMethod constructor initializes a new instance of the HttpMethod with an HTTP method that the app specifies.
Which is of course not possible with an enum.
See its constructor and method property.
来源:https://stackoverflow.com/questions/39717516/why-is-system-net-http-httpmethod-a-class-not-an-enum