回应HEAD请求在asp.NET MVC 3 [英] Responding to HEAD Request in asp.NET MVC 3

查看:76
本文介绍了回应HEAD请求在asp.NET MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有Asp.NET MVC 3的方式在一个通用的方式回应HEAD请求,而不是添加HEAD属性各个方法。

Is there a way in Asp.NET MVC 3 to respond to HEAD requests in a generic way, as opposed to adding the HEAD attribute to individual methods.

推荐答案

创建一个路由 RouteConstraint 像这样:

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";
    }
}

放置的路由处或​​附近的路线的顶部。当一个HEAD请求时,它会被路由到HeadController的指标作用。

Place the route at or near the top of your routes. When a HEAD request comes in, it will be routed to HeadController's Index action.

这篇关于回应HEAD请求在asp.NET MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆