Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 617
DavGarcia Avatar asked Jul 05 '10 18:07

DavGarcia


People also ask

Does Head request in HTTP return response body?

The HTTP HEAD and GET methods are identical, except that for HEAD requests, the server does not return a response body but still specifies the size of the response content using the Content-Length header.

How do I make a HTTP HEAD request?

To make a HEAD request with Curl, you need to use the -I or --head command-line parameter. The -I command-line parameter tells Curl to send an HTTP HEAD request to receive only HTTP headers. The HEAD request is very similar to a GET request, except that the server only returns HTTP headers without a response body.

What is HTTP HEAD request?

The HTTP HEAD method requests the headers that would be returned if the HEAD request's URL was instead requested with the HTTP GET method. For example, if a URL might produce a large download, a HEAD request could read its Content-Length header to check the filesize without actually downloading the file.


2 Answers

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();     } } 
like image 120
a7drew Avatar answered Sep 27 '22 21:09

a7drew


You can achieve the result by simply doing following

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)] public ActionResult TestMe() =>View(); 
like image 33
user2101889 Avatar answered Sep 27 '22 23:09

user2101889