当前http上下文的Http动词

你如何找到用于访问你的应用程序的http动词(POST,GET,DELETE,PUT)? 我正在寻找httpcontext.current,但似乎有任何属性给我信息。 谢谢

使用HttpContext.Current.Request.HttpMethod

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

 HttpContext.Current.Request.HttpMethod 

获得Get和Post

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

您还可以使用: HttpContext.Current.Request.RequestType

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

在ASP.NET CORE 2.0中,您可以使用以下方法获取(或设置)当前上下文的HTTP谓词:

 Request.HttpContext.Request.Method 

HttpContext.Current.Request.HttpMethod返回字符串,但最好使用枚举HttpVerbs 。 似乎没有构建方法来将currrent动词作为枚举,所以我为它编写了帮助器

助手class

 public static class HttpVerbsHelper { private static readonly Dictionary Verbs = new Dictionary() { {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(); } } 

基本控制器类的应用程序

 public abstract class BaseAppController : Controller { protected HttpVerbs? HttpVerb { get { var httpMethodOverride = ControllerContext.HttpContext.Request.GetHttpMethodOverride(); return HttpVerbsHelper.GetVerb(httpMethodOverride); } } }