为什么 System.Web.Mvc.HttpVerbs 类缺少 TRACE、CONNECT 和选项?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终创建了我自己的枚举并使用它。
MVC 似乎仍然可以处理 OPTIONS 类型的请求。
Ended up creating my own enum and using that.
MVC still seems to handle requests of type OPTIONS.