asp.net MvcHandler.ProcessRequest is never called

不问归期 提交于 2019-12-12 01:54:55

问题


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

    }

回答1:


You should override BeginProcessRequest, which has the following signature:

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


来源:https://stackoverflow.com/questions/7637295/asp-net-mvchandler-processrequest-is-never-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!