asp.net MvcHandler.ProcessRequest 从未被调用
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该重写
BeginProcessRequest
,它具有以下签名:You should override
BeginProcessRequest
, which has the following signature: