是否可以在网站的各个部分中定义路线。
例如,如果我想将网站功能分解为模块,每个模块都将定义所需的路由。
这可能吗?怎么样?
最佳答案
考虑使用ASP.NET MVC的内置区域。
本质上,“区域”是您的模块,“区域”使您可以为每个特定区域注册路由。
如果您之前没有使用过它们,则这里有MSDN演练:
http://msdn.microsoft.com/en-us/library/ee671793.aspx
本质上,每个区域都有一个目录,其中包含所有特定于区域的控制器和视图,并且在该目录的路由中放置一个文件,该文件注册该特定区域的路由,如下所示:
public class MyAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "My Area"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"news-articles",
"my-area/articles/after/{date}",
new {controller = "MyAreaArticles", action = "After"}
);
// And so on ...
}
}
您需要在global.asax.cs中注册所有这些额外的区域,以及其他主要路线:
public static void RegisterRoutes(RouteCollection routes)
{
AreaRegistration.RegisterAllAreas();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Products",
"products/show/{name}",
new {controller = "Products", action = "Show", name = UrlParameter.Optional}
);
...
}
关于c# - 如何在多个位置定义路线?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6933465/