Responding to HTTP HEAD Requests Using ASP.NET MVC

I would like to properly support the HTTP HEAD request when bots hit my ASP.NET MVC site using HEAD. I was informed that all HTTP HEAD requests to the site returned 404, especially from http://downforeveryoneorjustme.com . This is really annoying. Wish they switched to GET, like all other good bots.

If I just changed [AcceptVerbs(HttpVerbs.Get)] to [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)] , does MVC know to remove the request body?

What have you done to support HTTP HEAD requests? (The sample code will be wonderful!)

+28
asp.net-mvc acceptverbs
Jul 05 '10 at 18:29
source share
2 answers

I created a simple way of doing things in an ASP.Net MVC 2 project:

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

Then I ran Fiddler and created an HTTP GET request to click on this URL:

http://localhost.cla1149/Home/TestMe

The expected full page content has been returned.

Then I changed the request to use HTTP HEAD instead of HTTP GET . I received only the expected head information and information about the absence in the source file.

 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 

I assume that you include an action method restriction, so that it will only respond to HTTP GET . If you do something like this, it will work for both GET and HEAD , or you can completely omit the restriction if it doesn't give a value.

 public class HomeController : Controller { [AcceptVerbs(new[] {"GET", "HEAD"})] public ActionResult TestMe() { return View(); } } 
+45
Jul 07 2018-10-10T00:
source

You can achieve the result simply by doing the following

 [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)] public ActionResult TestMe() =>View(); 
+20
Feb 23 '13 at 8:11
source



All Articles