Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 views
in Technique[技术] by (71.8m points)

subdomain - asp.net MvcHandler.ProcessRequest is never called

On Asp.Net MVC 3 I have overwritten the MvcRouteHandler and MvcHandler to include handling the subdomain part of the Url.

However it never seems to call ProcessRequest method of MvcHandler.

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();

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should override BeginProcessRequest, which has the following signature:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...