Search code examples
c#asp.net-mvcasp.net-mvc-routingt4mvc

Map routes only for one controller and actions of this controller. Others routing settings leave operable


First of all, I need to say that I'm using T4MVC. I have many custom routes in my RouteConfig. This is an example:

    routes.MapRoute("CollectionDetails", "collection/{slug}/{collectionId}", MVC.Collection.CollectionDetails());    
..............................................                     
    routes.MapRoute("Sales.Index", "sales", MVC.Sales.Index());
    routes.MapRoute("CustomPage", "custom/{slug}", MVC.CustomPage.Index());

All these routes are working fine. But I have one controller (AccountController) for which I need to map routes in such scheme: ControllerName/ActionName/. I mean that I have Account Controller. This controller has such Actions: CreateAccount, CreateAccount(POST), ResetPassword, LogIn, etc... I need to create such Urls for them: Account/CreateAccount, Account/LogIn. I wonder is it possible to resolve using one route in RouteConfig?


Solution

  • If you want to lock a route to a specific Controller, (in your case, to just the Account Controller) then you can do it like this with T4MVC:

    From T4MVC Documentation: 2.2.5 routes.MapRoute

    routes.MapRoute(
         "Account",
         "Account/{action}/{id}",
          MVC.Account.Index(null));
    

    otherwise you could also achieve the same thing with

    from documentation 2.3. Use constants to refer to area, controller, action and parameter names

    routes.MapRoute(
         name: "Account",
         url: "Account/{action}/{id}",
         defaults: new { controller = MVC.Account.Name, action = MVC.Account.ActionNames.Index, id = UrlParameter.Optional }
    );
    

    Or go back to the default way of doing it

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

    This should satisfy your requirements.