Why is System.Net.Http.HttpMethod a class, not an enum?
Asked Answered
V

3

31

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 (HttpMethod.Get, HttpMethod.Put etc). What is the rationale behind not implementing HttpMethod as an enum?

Valval answered 27/9, 2016 at 6:29 Comment(2)
Are you asking that it could have been an enum and loose the methods it has to some other class?Blowfish
It could have been an enum, but then it could not support other http verbs than the standard one. There's actually a HttpMethod enum in the System.Net.Cache namespace.Gardell
G
40

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.

Gardell answered 27/9, 2016 at 6:40 Comment(2)
Ah, that explains it. My question was raised mainly because I reasoned that "all the allowed verbs are specified in the standard - why not just enumerate them?", but support for nonstandard verbs is, I guess, a sound consideration here. Thanks for clarifying!Valval
Some additional resources about using classes with static members as an alternative to enums: ardalis.com/enum-alternatives-in-c learn.microsoft.com/en-us/dotnet/architecture/microservices/…Greenes
A
8

After years, I suppose Microsoft has heard your wish...

Here is what you wished to have...

HttpVerb

Alloplasm answered 18/8, 2021 at 13:5 Comment(1)
Note: Requires Microsoft.AspNet.MvcGreenes
R
0

For ASP.NET Core, you have the following enum

Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpMethod
Reserved answered 19/6, 2023 at 23:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.