2014-11-05 87 views
0

我在注册路线中添加了新路线。添加新路线时出现异常

routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
routes.IgnoreRoute("{resource}.wdgt/{*pathInfo}"); 
routes.IgnoreRoute("ChartImg.axd/{*pathInfo}"); 
routes.Ignore("{*pathInfo}", new { pathInfo = @"^.*(ChartImg.axd)$" }); 
routes.IgnoreRoute("{resource}.svc"); 

routes.MapRoute(
    name: "DefaultWithTenantCode", 
    url: "{tenantcode}/{controller}/{action}/{id}", 
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, tenantcode = UrlParameter.Optional } 
      ); 

routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 

现在我试图访问下面的URL,我得到以下异常。 本地主机:53643 /帐户/ LogOn支持

例外: System.Web.HttpException(0X80004005):控制器用于路径 '/帐户/ LogOn支持' 未找到或没有实现一个IController。 在System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(的RequestContext的RequestContext,类型controllerType) 在System.Web.Mvc.DefaultControllerFactory.CreateController(的RequestContext的RequestContext,字符串controllerName) 在System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase HttpContext的,一个IController &控制器,IControllerFactory &工厂) 在System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase HttpContext的,AsyncCallback的回调,对象状态) 在System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext的HttpContext的,AsyncCallback的回调,对象状态) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context,AsyncCallback cb,Object extraData) 在System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布尔& completedSynchronously)}

请帮我解决这个问题。

在此先感谢。

回答

0

问题是,你已经声明了两条路径,两者都匹配,因此.NET路由将无法区分它们之间的区别。当您导航到/Account/Logon时,它会将DefaultWithTenantCode路线而不是Default路线与以下路线值进行匹配。

tenantCode = "Account" 
controller = "LogOn" 
action = "Index" 
id = "" 

一种摆脱这种情况的方法 - 在路由中添加某种标识符,以便知道何时传递租户代码。

routes.MapRoute(
    name: "DefaultWithTenantCode", 
    url: "tenant-code-{tenantcode}/{controller}/{action}/{id}", 
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, tenantcode = UrlParameter.Optional } 
     ); 

匹配DefaultWithTenantCode路线时,该会希望像/tenant-code-21/Tenant/Index/的URL。如果它不是以租户代码开始,它将使用Default路由。

另一种选择是在传递租户代码时制作所有需要的参数。

routes.MapRoute(
    name: "DefaultWithTenantCode", 
    url: "{tenantcode}/{controller}/{action}/{id}" 
     ); 

这将意味着它永远不会匹配DefaultWithTenantCode路线,除非所有4个参数被明确地传递。

相关问题