MVC3路由器
- C# code
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); List<string> language = Common.XMLHelper.GetValueAll_(); foreach (string str in language) { routes.MapRoute(Guid.NewGuid().ToString().Substring(0, 5), "{languages}", new { controller = "Book", action = "Index", languages = str }); } routes.MapRoute( "Default", // 路由名称 "{controller}/{action}/{id}", // 带有参数的 URL new { controller = "Book", action = "Index", id = UrlParameter.Optional } // 参数默认值 ); }
请教路由规则问题List<string> 是一个类似en-us、zh-cn的泛型集合,为什么我在浏览器里面输入www.123.com/zh-cnnnnnn也能进入到Action里面呢?
[解决办法]
routes.MapRoute(Guid.NewGuid().ToString().Substring(0, 5), "{languages}", new { controller = "Book", action = "Index", languages = str });
这样只是指定languages的缺省值,如果希望只允许en-us, zh-cn,用这个试试:
routes.MapRoute(Guid.NewGuid().ToString().Substring(0, 5), str , new { controller = "Book", action = "Index"});