问题
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