使用 ASP.NET MVC 响应 HTTP HEAD 请求
当机器人使用 HEAD 访问我的 ASP.NET MVC 站点时,我希望正确支持 HTTP HEAD 请求。我注意到,对该网站的所有 HTTP HEAD 请求都返回 404,特别是来自 http://downforeveryoneorjustme.com 。这真的很烦人。希望他们能像其他所有优秀的机器人一样切换到 GET。
如果我只是将 [AcceptVerbs(HttpVerbs.Get)]
更改为 [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
MVC 会知道删除请求正文吗?
您做了什么来支持 HTTP HEAD 请求? (代码示例会很棒!)
I'd like to correctly support the HTTP HEAD request when bots hit my ASP.NET MVC site using HEAD. It was brought to my attention that all HTTP HEAD requests to the site were returning 404s, particularly from http://downforeveryoneorjustme.com. Which is really annoying. Wish they would switch to GET like all the other good bots out there.
If I just change [AcceptVerbs(HttpVerbs.Get)]
to [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
will MVC know to drop the body of the request?
What have you done to support HTTP HEAD requests? (Code sample would be great!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 ASP.Net MVC 2 项目中创建了一个简单的操作方法:
然后启动 Fiddler 并构建了一个 HTTP GET 请求来访问此 URL:
http://localhost.:51149/Home/TestMe
返回了预期的全页内容。
然后,我将请求更改为使用 HTTP HEAD 而不是 HTTP GET。我在原始输出中只收到了预期的头部信息,没有主体信息。
我的猜测是,您对操作方法添加了约束,使其仅响应 HTTP GET 动词。如果您执行类似的操作,它将适用于 GET 和 HEAD,或者如果它不提供任何值,您可以完全忽略该约束。
I created a simple action method in an ASP.Net MVC 2 project:
Then I launched Fiddler and built up an HTTP GET request to hit this URL:
http://localhost.:51149/Home/TestMe
The expected full page content was returned.
Then, I changed the request to use an HTTP HEAD instead of an HTTP GET. I received just the expected head info and no body info in the raw output.
My guess is that you are including a constraint on the action method such that it will only respond to HTTP GET verbs. If you do something like this, it will work for both GET and HEAD, or you can omit the constraint entirely if it provides no value.
您只需执行以下操作即可获得结果
You can achieve the result by simply doing following