標籤:style blog http color strong io
routes.MapRoute( "Default_a", "huhangfei/{pageindex}-{state}-{size}.html", new { controller = "Home", action = "Index", pageindex = 1, state = 0 ,size=0}, new { pageindex = @"\d+", state = @"\d+", size = @"\d+" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
大家在學習MVC的過程,老是用到設定路由,但有6個常用路由,是大家經常用到的。
一.預設路由(MVC內建)
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // 路由名稱 "{controller}/{action}/{id}", // 帶有參數的 URL new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 參數預設值 (UrlParameter.Optional-可選的意思) ); }
二.不帶參數的路由
routes.MapRoute("NoParameter", "{controller}/{action}/{id}");
三.帶命名空間的路由
routes.MapRoute( "AdminControllers", // 路由名稱 "{controller}/{id}-{action}", // 帶有參數的 URL new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 參數預設值 new string[] { "Admin.Controllers" }//命名空間 );
四.帶約束的路由規則(約束的意思就是用正則這類約束必須符合條件才可以)
routes.MapRoute( "RuleControllers", "{controller}/{action}-{Year}-{Month}-{Day}}", new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" }, new { Year = @"^\d{4}", Month = @"\d{2}" } //4位元 2位元 );
五.帶名稱空間,帶約束,帶預設值的路由規則
routes.MapRoute( "Rule1", "Admin/{controller}/{action}-{Year}-{Month}-{Day}", new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" }, new { Year = @"^\d{4}", Month = @"\d{2}" }, new string[] { "Admin.Controllers" } );
六.捕獲所有的路由
routes.MapRoute( "All", // 路由名稱 "{*Vauler}", // 帶有參數的 URL new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 參數預設值 );