【发布时间】:2018-05-20 13:03:58
【问题描述】:
我已将我的项目重新组织成更合乎逻辑的层次结构:
-Controllers
-Accounts
-CustomersController
-Setup
-SystemDefaultsController
-SettingsController
-HomeController
目前,我只是尝试设置我的 URL 以匹配此结构。因此,有效的示例 URL 将是:
localhost:1234/Home/Index
localhost:1234/Setup/SystemDefaults/Index
localhost:1234/Setup/Settings/Index
localhost:1234/CustomerAccounts/Index
所以我在 RouteConfig.cs 中的默认路由之上添加了一个路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes();
//AreaRegistration.RegisterAllAreas();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Setup",
url: "Setup/{controller}/{action}/{id}",
defaults: new { controller = "Setup", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MVCWeb.Controllers.Setup" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MVCWeb.Controllers" }
);
}
这确实使上述 URL 工作,但它没有任何限制,因此当我希望这些 URL 无效时,这些 URL 工作:
localhost:1234/Setup/CustomerAccounts/Index localhost:1234/SystemDefaults/索引
此外,由于 Setup 路由匹配所有内容,如果我执行以下任一操作:
@Html.ActionLink("Customer Accounts", "Index", "CustomerAccounts")
@Url.Action("Index", "CustomerAccounts")
它将 URL 生成为 /Setup/CustomerAccounts/Index 而不是 /CustomerAccounts/Index。
在路由 URL 中仍然使用 {controller} 的同时,有没有办法在不使用区域的情况下完成此操作?还是我必须在设置下为每个控制器专门添加一个路由?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-5 routes url-routing