Cha*_*ell 11 asp.net-mvc routing
我已经看过StackOverflow上的路由,我有一个非常noobie的问题,但我想澄清一点.
我正在专门研究用户控制器
https://stackoverflow.com/Users
https://stackoverflow.com/Users/Login
https://stackoverflow.com/Users/124069/rockinthesixstring
我注意到的是,"用户"控制器可能具有默认的"索引"操作和"登录"操作.我面临的问题是可以忽略登录操作,也可以使用"UrlParameter.Optional [ID]".
这在RegisterRoutes集合中看起来究竟如何?还是我错过了一些完全明显的东西?
编辑:这是我目前的路线..但它肯定远非正确.
routes.MapRoute( _
"Default", _
"{controller}/{id}/{slug}", _
New With {.controller = "Events", .action = "Index", .id = UrlParameter.Optional, .slug = UrlParameter.Optional} _
)
Run Code Online (Sandbox Code Playgroud)
可能只是使用一个特定的路由来处理它,也使用正则表达式来指定ID的格式(因此它不会与在该位置包含动作名称的其他路由混淆).
// one route for details
routes.MapRoute("UserProfile",
"Users/{id}/{slug}",
new { controller = "Users", action = "Details", slug = string.Empty },
new { id = @"\d+" }
);
// one route for everything else
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
Run Code Online (Sandbox Code Playgroud)