ProcessRequest不会被调用

ProcessRequest不会被调用

本文介绍了asp.net MvcHandler.ProcessRequest不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Asp.Net MVC 3我已覆盖MvcRouteHandler和MvcHandler包括处理URL的子域部分。

但它似乎永远不会调用MvcHandler的ProcessRequest方法。

 公共类SubDomainMvcRouteHandler:MvcRouteHandler
{
    保护覆盖的IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext的RequestContext)
    {
        返回新SubDomainMvcHandler(RequestContext的);
    }
}


 公共类SubDomainMvcHandler:MvcHandler
{
    公共SubDomainMvcHandler(RequestContext的上下文)
        :基座(上下文)
    {
    }    保护覆盖无效的ProcessRequest(HttpContextBase HttpContext的)
    {
        字符串[] = hostNameParts httpContext.Request.Url.Host.Split('。');        INT长度= hostNameParts.Length - 3;        的for(int i =长度; I> = 0;我 - )
        {
            如果(hostNameParts [I]!=WWW)
                RequestContext.RouteData.Values​​.Add(子域+(长度 - I + 1),hostNameParts [0]);
        }        base.ProcessRequest(HttpContext的);
    }
}


 公共静态无效的RegisterRoutes(RouteCollection路线)
    {
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        routes.MapRoute(
            默认,//路线名称
            {控制器} / {行动} / {ID},// URL带参数
            新{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
        ).RouteHandler =新SubDomainMvcRouteHandler();    }


解决方案

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

 保护覆盖的IAsyncResult BeginProcessRequest(HttpContextBase的HttpContext,AsyncCallback的回调,对象的状态)

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

    }
解决方案

You should override BeginProcessRequest, which has the following signature:

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

这篇关于asp.net MvcHandler.ProcessRequest不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:09