Reputation: 9296
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
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
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
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
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
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
Reputation: 1190
For getting Get and Post
string method = HttpContext.Request.HttpMethod.ToUpper();
Upvotes: 2
Reputation: 190996
Use HttpContext.Current.Request.HttpMethod
.
See: http://msdn.microsoft.com/en-us/library/system.web.httprequest.httpmethod.aspx
Upvotes: 50