为什么 System.Web.Mvc.HttpVerbs 类缺少 TRACE、CONNECT 和选项?

发布于 2025-01-06 00:48:01 字数 1244 浏览 8 评论 0原文

RFC 2616 HTTP/1.1 定义指出存在以下常见 HTTP 方法:

GET、HEAD、POST、PUT、DELETE、TRACE、OPTIONS、CONNECT

但是 System.Web.Mvc.HttpVerbs 枚举缺少 TRACE、OPTIONS 和连接。

我有一个动作过滤器,它从请求中获取 HttpVerb 以便做出某些决定(例如,如果请求 PUT 基本类型 A 的模型,那么我设置一些数据),所以这个实用程序代码对最后三个请求抛出 ArgumentOutOfRangeException(主要是选项 - 看起来像是来自 Google 翻译):

public static HttpVerbs GetHttpVerb(this HttpRequestBase httpRequestBase)
{
    switch (httpRequestBase.HttpMethod)
    {
        case "GET":
            return HttpVerbs.Get;
        case "POST":
            return HttpVerbs.Post;
        case "PUT":
            return HttpVerbs.Put;
        case "DELETE":
            return HttpVerbs.Delete;
        case "HEAD":
            return HttpVerbs.Head;    
        default:
            throw new ArgumentOutOfRangeException("httpRequestBase");
    }
}

不确定如何解决这个问题 - 有什么想法吗?

我唯一能想到的是更改所有引用代码以首先检查原始 HTTP 方法,然后仅在不是 TRACE、OPTIONS 或 CONNECT 时调用该实用程序。这有点hacky。

为什么枚举类中缺少它?有什么具体原因吗? MVC 不能处理这些类型的请求吗?

从 OPTIONS 方法的声音来看,它甚至不应该到达 MVC,并且应该由 IIS 本身处理?

The RFC 2616 HTTP/1.1 definition states that the following common HTTP methods exist:

GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT

But the System.Web.Mvc.HttpVerbs enum is missing TRACE, OPTIONS & CONNECT.

I've got an action filter which picks up the HttpVerb from the request in order to make certain decisions (for example, if a request is PUT'ing a model of base type A, then i set some data), so this utility code is throwing an ArgumentOutOfRangeException for requests of the last three (mostly OPTIONS - looks like it's coming from Google Translate):

public static HttpVerbs GetHttpVerb(this HttpRequestBase httpRequestBase)
{
    switch (httpRequestBase.HttpMethod)
    {
        case "GET":
            return HttpVerbs.Get;
        case "POST":
            return HttpVerbs.Post;
        case "PUT":
            return HttpVerbs.Put;
        case "DELETE":
            return HttpVerbs.Delete;
        case "HEAD":
            return HttpVerbs.Head;    
        default:
            throw new ArgumentOutOfRangeException("httpRequestBase");
    }
}

Not sure how to get around this - any ideas?

Only thing i can think of is change all referencing code to check the raw HTTP method first, then only call the utility if it's not TRACE, OPTIONS or CONNECT. Which is kinda hacky.

Why is it missing from the enum class? Is there a specific reason for it? Can MVC simply not handle requests of these types?

By the sounds of the OPTIONS method, it shouldn't even get to MVC, and it should be handled by IIS itself?

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

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

发布评论

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

评论(1

浅笑轻吟梦一曲 2025-01-13 00:48:01

最终创建了我自己的枚举并使用它。

public enum HttpVerb
{
    Get,
    Head,
    Post,
    Put,
    Delete,
    Trace,
    Options,
    Connect
}

MVC 似乎仍然可以处理 OPTIONS 类型的请求。

Ended up creating my own enum and using that.

public enum HttpVerb
{
    Get,
    Head,
    Post,
    Put,
    Delete,
    Trace,
    Options,
    Connect
}

MVC still seems to handle requests of type OPTIONS.

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