当前 http 上下文的 HTTP 动词

发布于 2024-11-27 05:15:54 字数 201 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

有木有妳兜一样 2024-12-04 05:15:54

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

Request.HttpContext.Request.Method

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

Request.HttpContext.Request.Method
窗影残 2024-12-04 05:15:54
HttpContext.Current.Request.HttpMethod
HttpContext.Current.Request.HttpMethod
心欲静而疯不止 2024-12-04 05:15:54
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
}
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
}
誰ツ都不明白 2024-12-04 05:15:54

用于获取 Get 和 Post

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

For getting Get and Post

string method = HttpContext.Request.HttpMethod.ToUpper();
〆一缕阳光ご 2024-12-04 05:15:54

在 ASP.NET Core v3.1 中,我使用注入到构造函数中的 HttpContextAccessor 接口检索当前的 HTTP 动词,例如

private readonly IHttpContextAccessor _httpContextAccessor;

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

用法:

_httpContextAccessor.HttpContext.Request.Method

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
桃扇骨 2024-12-04 05:15:54

HttpContext.Current.Request.HttpMethod 返回字符串,但最好使用枚举 HttpVerbs。似乎没有内置方法来获取当前动词作为枚举,所以我为它编写了助手应用程序的助手

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

基控制器类

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

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);
            }
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文