Tom Squires
Tom Squires

Reputation: 9296

Http verb of current http context

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

Upvotes: 39

Views: 26910

Answers (8)

Rdwan Alali
Rdwan Alali

Reputation: 189

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
}

Upvotes: 6

Ciaran Gallagher
Ciaran Gallagher

Reputation: 4020

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

Upvotes: 2

Ben Smith
Ben Smith

Reputation: 20230

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

Request.HttpContext.Request.Method

Upvotes: 9

Sel
Sel

Reputation: 2004

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);
            }
        }
}

Upvotes: 1

Frank Hale
Frank Hale

Reputation: 1896

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

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

Upvotes: 2

Kishan
Kishan

Reputation: 1190

For getting Get and Post

string method = HttpContext.Request.HttpMethod.ToUpper();

Upvotes: 2

spender
spender

Reputation: 120508

HttpContext.Current.Request.HttpMethod

Upvotes: 7

Daniel A. White
Daniel A. White

Reputation: 190996

Use HttpContext.Current.Request.HttpMethod.

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

Upvotes: 50

Related Questions