使用ASP响应HTTP HEAD请求。NET MVC

时间:2021-01-26 04:11:59

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.

我想在机器人攻击我的ASP时正确地支持HTTP HEAD请求。NET MVC站点使用HEAD。我注意到所有的HTTP头请求都返回了404s,特别是来自http://downforeveryoneorjustme.com。这是很烦人的。希望他们能像其他优秀的机器人一样。

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

如果我将[acceptverb (http动词. get)]改为[acceptverb (http谓词)。获取| http动词. head) MVC会知道删除请求体吗?

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

您如何支持HTTP HEAD请求?(代码样本会很棒!)

2 个解决方案

#1


46  

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

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

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

Then I launched Fiddler and built up an HTTP GET request to hit this URL:

然后我启动了Fiddler并构建了一个HTTP GET请求来访问这个URL:

http://localhost.:51149/Home/TestMe

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.

然后,我将请求改为使用HTTP HEAD而不是HTTP GET。我只收到预期的头部信息,在原始输出中没有身体信息。

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

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.

我的猜测是,您在action方法上包含了一个约束,以便它只响应HTTP GET谓词。如果您这样做,它将对GET和HEAD都起作用,或者您可以完全忽略约束,如果它没有提供任何值。

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

#2


21  

You can achieve the result by simply doing following

您可以通过简单地执行以下操作来获得结果

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
public ActionResult TestMe() =>View();

#1


46  

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

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

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

Then I launched Fiddler and built up an HTTP GET request to hit this URL:

然后我启动了Fiddler并构建了一个HTTP GET请求来访问这个URL:

http://localhost.:51149/Home/TestMe

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.

然后,我将请求改为使用HTTP HEAD而不是HTTP GET。我只收到预期的头部信息,在原始输出中没有身体信息。

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

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.

我的猜测是,您在action方法上包含了一个约束,以便它只响应HTTP GET谓词。如果您这样做,它将对GET和HEAD都起作用,或者您可以完全忽略约束,如果它没有提供任何值。

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

#2


21  

You can achieve the result by simply doing following

您可以通过简单地执行以下操作来获得结果

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
public ActionResult TestMe() =>View();