Responding to a HEAD request in asp.NET MVC 3

Is there a way in Asp.NET MVC 3 to respond to HEAD requests in a general way, rather than adding the HEAD attribute to individual methods.

+3
asp.net-mvc
source share
1 answer

Create a route with RouteConstraint as follows:

 routes.MapRoute( "HEAD Requests", "{*fullPath}", new { controller = "Head", action = "Index" }, new { fullPath = new MustBeHeadRequest() } ); public class MustBeHeadRequest : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { return httpContext.Request.HttpMethod.ToLowerInvariant() == "head"; } } 

Place the route at or near the top of your routes. When a HEAD request is requested, it will be redirected to the action of the HeadController pointer.

+4
source share

All Articles