.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查找确定。
- 从MVC 4 Web Api返回匿名类型失败,出现序列化错误
- 通过HttpClient使用外部REST Web服务的存储库模式示例?
- 如何在Visual Studio 2012中配置/设置Log4Net与ASP.NET(包含MVC)C#
- 参数超出EF中的db.SaveChanges()范围
- 如何使用C#和asp.net创建带有用户名的子域?
- ASP.NET MVC:DropDownListFor不选择任何选项
- 如何返回JsonResult
- PrincipalContext.ValidateCredentials始终返回FALSE
- 使用ASP.Net MVC RouteConfig的AngularJS Ui-Router。 它是如何工作的?