.NET MVC自定义路由

我想知道我是否可以创建一个比控制器更高级别的路由映射。 典型的路由包括“/ controller / action / id”。 我要找的是“section / controller / action / id”或“controller / section / action / id”。 我怎样才能做到这一点?

没问题。 例如,只需创建一个URL,其路由就是

path/to/my/application/{controller}/{action}/{id}

…并像往常一样提供默认控制器和操作。

一个具体的例子是

 context.MapRoute( "Admin_default", "admin/{controller}/{action}/{id}", new { controller = "AdminHome", action = "Index", id = "" } ); 

例如,这将映射以下URL:

 /admin/ => AdminHomeController.Index /admin/adminhome/ => AdminHomeController.Index /admin/other/ => OtherController.Index /admin/statistics/view/50 => StatisticsController.View(50) 

但请注意,如果您还有默认路由,例如:

 context.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } ); 

…然后,管理员路由中的控制器操作方法也可以通过此路由访问。 使用URL Routing Debugger查找确定。