仅为一个控制器和该控制器的操作映射路由。其他路由设置保持可操作

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

首先,我需要说明一下,我使用的是T4MVC。我的 RouteConfig 中有很多自定义路线。这是一个例子:

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

所有这些路由都运行良好。但是我有一个控制器(AccountController),我需要在这样的方案中为其映射路由:ControllerName/ActionName/。 我的意思是我有 Account Controller。这个控制器有这样的 ActionsCreateAccount, CreateAccount(POST), ResetPassword, LogIn, etc... 我需要为他们创建这样的 UrlsAccount/CreateAccountAccount/LogIn。我想知道是否可以在 RouteConfig 中使用一个 route 来解决?

使用默认 MVC 路由:

routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = MVC.Home.Name, action = MVC.Home.ActionNames.Index, id = "" }  // Parameter defaults
            );

你应该能够实现你想要的..

只要它在列表底部定义,您的海关路线就会在它之前触发,并且它们将继续工作。当涉及帐户 URLs 时,您的自定义路由将不匹配,它们将使用默认路由到帐户控制器和 URL.

中指定的操作

详细了解 MVC 路由 http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs

如果您想锁定到特定控制器的路由(在您的情况下,仅锁定到帐户控制器),那么您可以使用 T4MVC 这样做:

来自 T4MVC 文档:2.2.5 routes.MapRoute

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

否则你也可以用

达到同样的效果

来自文档 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 }
);

或者回到默认的方式

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

这应该可以满足您的要求。