104

I need to check the RequestType of an HttpRequest in ASP.NET (or WebRequest.Method). I know that I can just use the string values "POST" or "GET" for the request type, but I could have sworn there was a constant somewhere in some class in .NET that contained the values.

Out of curiosity I was wondering if anyone knew what class these string constants for GET and POST were in. I've tried searching online but I've had no luck, so I thought I'd ask here.

4 Answers 4

136
System.Net.WebRequestMethods.Http
    .Connect = "CONNECT"
    .Get = "GET"
    .Head = "HEAD"
    .MkCol = "MKCOL"
    .Post = "POST"
    .Put = "PUT"

Ultimately, though; since const expressions are burned into the caller, this is identical to using "GET" etc, just without the risk of a typo.

3
  • 27
    Interesting, why there is no DELETE method and "*"? Feb 17, 2011 at 23:51
  • 11
    And it's missing OPTIONS and TRACE. Jun 15, 2012 at 15:34
  • 7
    PATCH verb is also missing.
    – RBT
    Mar 26, 2017 at 10:45
61

Also exists System.Net.Http.HttpMethod which can serve instead of enum. You can compare them aMethod == HttpMethod.Get, etc. To get string method name call e.g. HttpMethod.Get.Method.

5
  • 7
    This has the advantage to work in Asp.Net Core as well
    – superjos
    May 17, 2016 at 16:05
  • 3
    I'm just wondering why they haven't declared those fields as constants instead of static readonly fields. The advantage of constants is that you can pass them as parameter defaults (i.e. void Request(HttpMethod method = HttpMethod.Get)). Feb 18, 2017 at 19:39
  • 1
    @Shimmy The disadvantage of constants are that they're baked into the referencing assembly at compile-time via simple substitution. That means that if the assembly that defines the constants changes their values and is replaced, any referencing assemblies will retain the original, no wrong, constant values. How likely it is that HTTP methods will ever change, however, is another story...
    – Ian Kemp
    Apr 19, 2017 at 8:02
  • 1
    HttpMethod.Get.Method is NOT a compile time constant :( where System.Net.WebRequestMethods.Http are constants
    – Piotr Kula
    Dec 21, 2017 at 10:25
  • 1
    The need for compile time constants is unavoidable regardless their advantages or disadvantages. For example, default parameters in a method signature. When I can not use System.Net.WebRequestMethods, I usually work around this by passing strings HttpMethod can use in its constructor. The trouble is, of course, their documentation doesn't explicitly say what happens when a bad string value is passed in so use this strategy at your own risk, learn.microsoft.com/en-us/dotnet/api/… Jun 25, 2019 at 18:01
17

In ASP.NET MVC they're in System.Web.Mvc.HttpVerbs. But all methods that take one of these enum values also has a text override, as there is no complete set of HTTP verbs, only a set of currently defined values (see here and here and here).

You can't create an enumeration that covers all verbs, as there is the possibility that verbs can be added, and enumerations have versioning issues that make this impractical.

12

In ASP.NET Core you will find a collection of http method strings in the HttpMethods.cs class under the Microsoft.AspNetCore.Http namespace.

This class also offers boolean helpers such as IsGet() or IsPost() for better semantics.

Please note that these strings are exposed as public static readonly string and not as constants.

UPDATE 2020-05-17: GetCanonicalizedValue(string method) was added to the HttpMethods.cs class in ASP.NET Core v5, which returns the static instance equivalent to the provided string method name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.