36

How do you find the http verb (POST,GET,DELETE,PUT) used to access your application? Im looking httpcontext.current but there dosent seem to be any property that gives me the info. Thanks

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
Tom Squires
  • 8,848
  • 12
  • 46
  • 72

8 Answers8

49

Use HttpContext.Current.Request.HttpMethod.

See: http://msdn.microsoft.com/en-us/library/system.web.httprequest.httpmethod.aspx

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
8

In ASP.NET CORE 2.0 you can get (or set) the HTTP verb for the current context using:

Request.HttpContext.Request.Method
Ben Smith
  • 19,589
  • 6
  • 65
  • 93
7
HttpContext.Current.Request.HttpMethod
spender
  • 117,338
  • 33
  • 229
  • 351
5
if (HttpContext.Request.HttpMethod == HttpMethod.Post.Method) 
{
        // The action is a post 
}
    
if (HttpContext.Request.HttpMethod == HttpMethod.put.Method)
{
        // The action is a put 
}

if (HttpContext.Request.HttpMethod == HttpMethod.DELETE.Method)
{
        // The action is a DELETE
}

if (HttpContext.Request.HttpMethod == HttpMethod.Get.Method)
{
        // The action is a Get
}
DanB
  • 2,022
  • 1
  • 12
  • 24
Rdwan Alali
  • 149
  • 2
  • 1
2

For getting Get and Post

string method = HttpContext.Request.HttpMethod.ToUpper();
Kishan
  • 1,182
  • 12
  • 26
2

In ASP.NET Core v3.1 I retrieve the current HTTP verb by using the HttpContextAccessor interface which is injected in on the constructor, e.g.

private readonly IHttpContextAccessor _httpContextAccessor;

public MyPage(IHttpContextAccessor httpContextAccessor)
{
    _httpContextAccessor = httpContextAccessor;
}

Usage:

_httpContextAccessor.HttpContext.Request.Method
Ciaran Gallagher
  • 3,895
  • 9
  • 53
  • 97
2

You can also use: HttpContext.Current.Request.RequestType

https://msdn.microsoft.com/en-us/library/system.web.httprequest.requesttype(v=vs.110).aspx

AlignedDev
  • 8,102
  • 9
  • 56
  • 91
Frank Hale
  • 1,886
  • 1
  • 17
  • 31
0

HttpContext.Current.Request.HttpMethod return string, but better use enum HttpVerbs. It seems there are no build in method to get currrent verb as enum, so I wrote helper for it

Helper class

public static class HttpVerbsHelper
    {
        private static readonly Dictionary<HttpVerbs, string> Verbs =
            new Dictionary<HttpVerbs, string>()
            {
                {HttpVerbs.Get, "GET"},
                {HttpVerbs.Post, "POST"},
                {HttpVerbs.Put, "PUT"},
                {HttpVerbs.Delete, "DELETE"},
                {HttpVerbs.Head, "HEAD"},
                {HttpVerbs.Patch, "PATCH"},
                {HttpVerbs.Options, "OPTIONS"}
            };

        public static HttpVerbs? GetVerb(string value)
        {
            var verb = (
                from x in Verbs
                where string.Compare(value, x.Value, StringComparison.OrdinalIgnoreCase) == 0
                select x.Key);
            return verb.SingleOrDefault();
        }
    }

base controller class of application

public abstract class BaseAppController : Controller
    {
        protected HttpVerbs? HttpVerb
        {
            get
            {
                var httpMethodOverride = ControllerContext.HttpContext.Request.GetHttpMethodOverride();
                return HttpVerbsHelper.GetVerb(httpMethodOverride);
            }
        }
}
Sel
  • 1,934
  • 1
  • 22
  • 13