MVC 学习系列文章(二)URL Routing
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//URL Routing 是从上至下进行匹配,应按照范围从小至大进行书写
//httpMethod = "POST"
//Book/Search/2012-01-29
routes.MapRoute(
"Book",//名称
"Book/Search/{Date}",//规则
new { controller = "Book", action = "Search" },//默认
new { Date = @"^\d{4}-\d{2}-\d{2}" }//约束
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
对于URL Routing可使用RouteDebug进行测试。
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);//RouteDebug测试
}
若有疑问或不正之处,欢迎提出指正和讨论。