Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

acceptverbs - Respond to HTTP HEAD requests using ASP.NET MVC

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!)

question from:https://stackoverflow.com/questions/3181500/respond-to-http-head-requests-using-asp-net-mvc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...