开发者

asp.net MvcHandler.ProcessRequest is never called

开发者 https://www.devze.com 2023-04-10 01:04 出处:网络
On Asp.Net MVC 3 I have overwritten the MvcRouteHandler and MvcHandler to include handling the subdomain part of the Url.

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
{
    protec开发者_C百科ted 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();

    }


You should override BeginProcessRequest, which has the following signature:

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

精彩评论

暂无评论...
验证码 换一张
取 消