永远不会调用asp.net MvcHandler.ProcessRequest

时间:2011-10-03 15:43:16

标签: asp.net-mvc-3 subdomain asp.net-mvc-routing httprequest

在Asp.Net MVC 3上,我覆盖了MvcRouteHandler和MvcHandler以包含处理Url的子域部分。

然而,似乎永远不会调用MvcHandler的ProcessRequest方法。

public class SubDomainMvcRouteHandler : MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
    {
        return new SubDomainMvcHandler(requestContext);
    }
}

public class SubDomainMvcHandler : MvcHandler
{
    public SubDomainMvcHandler(RequestContext context)
        : base(context)
    {
    }

    protected override void ProcessRequest(HttpContextBase httpContext)
    {
        string[] hostNameParts = httpContext.Request.Url.Host.Split('.');

        int length = hostNameParts.Length - 3;

        for (int i = length; i >= 0; i--)
        {
            if (hostNameParts[i] != "www")
                RequestContext.RouteData.Values.Add("SubDomain" + (length - i + 1), hostNameParts[0]);
        }

        base.ProcessRequest(httpContext);
    }
}

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        ).RouteHandler = new SubDomainMvcRouteHandler();

    }

1 个答案:

答案 0 :(得分:1)

您应该覆盖BeginProcessRequest,其中包含以下签名:

protected override IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state)