Mic*_*cah 4 c# asp.net-mvc routing .net-4.0
我正在寻找符合这些模式的路线:
/users
Mapped to action GetAllUsers()
/users/12345
Mapped to action GetUser(int id)
/users/1235/favorites
mapped to action GetUserFavorites(int id)
Run Code Online (Sandbox Code Playgroud)
控制器应始终是UsersController.我认为这会奏效,但事实并非如此.
routes.MapRoute("1",
"{controller}/{action}/{id}",
new { id = UrlParameter.Optional, action = "index" });
routes.MapRoute("2",
"{controller}/{id}/{action}");
Run Code Online (Sandbox Code Playgroud)
我正在努力绕过它.任何帮助将非常感激.
cou*_*ben 10
为了实现您的目标,您需要RegisterRoutes在global.asax.cs中使用三个单独的路由,这些路由应按以下顺序添加,并且必须在Default路由之前(假设id必须是整数):
routes.MapRoute(
"GetUserFavorites", // Route name
"users/{id}/favorites", // URL with parameters
new { controller = "Users", action = "GetUserFavorites" }, // Parameter defaults
new { id = @"\d+" } // Route constraint
);
routes.MapRoute(
"GetUser", // Route name
"users/{id}", // URL with parameters
new { controller = "Users", action = "GetUser" } // Parameter defaults
new { id = @"\d+" } // Route constraint
);
routes.MapRoute(
"GetAllUsers", // Route name
"users", // URL with parameters
new { controller = "Users", action = "GetAllUsers" } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1499 次 |
| 最近记录: |