0
votes

I added new Route in Register Route.

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

Now I tried to access the below url, I am getting the following exception. localhost:53643/Account/LogOn

Exception: System.Web.HttpException (0x80004005): The controller for path '/Account/LogOn' was not found or does not implement IController. at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)}

Please help me to solve this issue.

Thanks in Advance.

1

1 Answers

0
votes

The problem is that you have declared 2 routes that will both match so .NET routing won't be able to tell the difference between them. When you navigate to /Account/Logon, it is matching the DefaultWithTenantCode route instead of the Default route with the following route values.

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

One way to get out of this scenario - add some kind of identifier to the route so it will know when you are passing a tenantcode.

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

This will expect a URL like /tenant-code-21/Tenant/Index/ when matching the DefaultWithTenantCode route. If it doesn't start with the tenant code, it will use the Default route.

Another option is to make all of the parameters required when passing a tenantcode.

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

This will mean it won't ever match the DefaultWithTenantCode route unless all 4 parameters are passed explicitly.