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
8 Answers
Use HttpContext.Current.Request.HttpMethod.
See: http://msdn.microsoft.com/en-us/library/system.web.httprequest.httpmethod.aspx
- 187,200
 - 47
 - 362
 - 445
 
In ASP.NET CORE 2.0 you can get (or set) the HTTP verb for the current context using:
Request.HttpContext.Request.Method
- 19,589
 - 6
 - 65
 - 93
 
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
}
- 2,022
 - 1
 - 12
 - 24
 
- 149
 - 2
 - 1
 
For getting Get and Post
string method = HttpContext.Request.HttpMethod.ToUpper();
- 1,182
 - 12
 - 26
 
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
- 3,895
 - 9
 - 53
 - 97
 
You can also use: HttpContext.Current.Request.RequestType
https://msdn.microsoft.com/en-us/library/system.web.httprequest.requesttype(v=vs.110).aspx
- 8,102
 - 9
 - 56
 - 91
 
- 1,886
 - 1
 - 17
 - 31
 
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);
            }
        }
}
- 1,934
 - 1
 - 22
 - 13