asp.net MvcHandler.ProcessRequest 从未被调用

发布于 2024-12-07 16:25:15 字数 1469 浏览 0 评论 0原文

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

    }

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

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2024-12-14 16:25:15

您应该重写 BeginProcessRequest,它具有以下签名:

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

You should override BeginProcessRequest, which has the following signature:

protected override IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文