本文介绍了应对使用ASP.NET MVC HTTP HEAD请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想正确支持HTTP HEAD请求时使用的机器人击中头部我的ASP.NET MVC的网站。它带给我的注意,所有的HTTP HEAD请求到该网站正在返回404错误,特别是来自。这实在是烦人。希望他们改用得到像其他所有好机器人那里。

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.

如果我只是改变的[AcceptVerbs(HttpVerbs.Get)] 的[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)] 将MVC知道砸请求的主体?

If I just change [AcceptVerbs(HttpVerbs.Get)] to [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)] will MVC know to drop the body of the request?

你做了什么,支持HTTP HEAD请求? (code样品将是巨大的!)

What have you done to support HTTP HEAD requests? (Code sample would be great!)

推荐答案

我在ASP.Net MVC 2项目创建了一个简单的操作方法:

I created a simple action method in an ASP.Net MVC 2 project:

public class HomeController : Controller
{
    public ActionResult TestMe()
    {
        return View();
    }
}

然后我发起小提琴手,并建立了一个 HTTP GET 要求打这个网址:

预期的整页内容被退回。

The expected full page content was returned.

然后,我改变了要求使用的 HTTP HEAD 而不是一个 HTTP GET 。我刚刚收到预期的头信息,并在原始输出没有任何机构的信息。

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.

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Wed, 07 Jul 2010 16:58:55 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 2.0
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 1120
Connection: Close

我的猜测是,你包括操作方法约束这样的,它只会为 HTTP GET 动词回应。如果你这样做,它会为获取 HEAD 工作,或者你可以完全忽略的约束,如果它提供了没有价值。

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.

public class HomeController : Controller
{
    [AcceptVerbs(new[] {"GET", "HEAD"})]
    public ActionResult TestMe()
    {
        return View();
    }
}

这篇关于应对使用ASP.NET MVC HTTP HEAD请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:04
查看更多