【基礎】MVC路由規則

RouteData解析過程

在ASP.NET MVC中,服務器收到來自客戶端的請求後,會通過一些列的處理拿到請求的數據,好比在Pipeline 管線事件中,經過訂閱適當的事件,將HttpContext做爲參數傳入HttpContextWrapper進行封裝,而後取得當前路由集合的數據RouteData進行解析,拿到具體的參數,包括請求路徑、請求的參數、IRouteHandler等,經過IRouteHandlerGetHttpHandler返回一個IHttpHandler對象,經過該對象的ProcessRequest對請求進行處理,而後控制器工廠經過RouteData中匹配的Controller進行反射構造一個ControllerController調用IControllerExcute方法,一樣是經過反射拿到當前請求的Action,最後執行Action,返回客戶端數據,完成本次的請求。總體流程圖以下所示:服務器

 

在這個過程當中,RouteData中的路由 起到了很大的做用。Routing的做用:首先經過HTTP請求,並解析Url請求中ControllerAction以及附加數據,其次將識別出來的數據傳遞給ControllerAction(Controller的方法)。這是Routing組件的兩個重要的做用!app

路由規則實例解析

實例一:系統默認提供的路由格式

下面是系統給的默認代碼:函數

 1 public static void RegisterRoutes(RouteCollection routes)
 2 {
 3     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 4 
 5     routes.MapRoute(
 6         "Default", // 路由名稱
 7         "{controller}/{action}/{id}", // 帶有參數的 URL
 8         new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 參數默認值
 9     );
10 }

Url格式爲:http://localhost:80/home/index  對應規則爲:{controller}/{action}/{id}  黑體部分就是對應部分。這仍是有默認值的狀況。
詳細匹配應該爲:http://localhost:80/Custom/Detials/1   。去掉主機域名,剩下的對應就是匹配Controller和Action了。經過Routing組件解析這個Url,Controller是Custom,Action是Detials。傳遞過去的Id是1。這就是使用了MapRoute( string name, string url, object defaults);這個方法的重載。測試

實例二:不使用默認值的Url路由規則

函數簽名:MapRoute( string name, string url);url

routes.MapRoute("沒有默認值路由規則", "{controller}/{id}-{action}");spa

適合的Url例子:http://localhost:80/Custom/1-Detials   調試

它將不匹配http://localhost:80/code

實例三:帶名稱空間的Url路由規則

函數簽名MapRoute( string name, string url, string[] namespaces);//路由名,Url規則,名稱空間對象

1 routes.MapRoute(
2             "MyUrl", // 路由名稱
3             "{controller}/{id}-{action}", // 帶有參數的 URL
4             new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // 參數默認值
5             new string[] { "MvcDemo.Controllers" }//命名空間
6 );

Url:http://localhost:0000/Custom/1-Detials blog

這個例子是帶命名空間的路由規則,這在Aeras使用時很是有用。

實例四:帶約束的路由規則

函數簽名:MapRoute( string name, string url, object defaults, object constraints);//路由名,Url規則,默認值,名稱空間

1 routes.MapRoute(
2              "Rule1",
3              "{controller}/{action}-{Year}-{Month}-{Day}}",
4              new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
5              new { Year = @"^\d{4}", Month = @"\d{2}" }
6 );

實例五:帶名稱空間,帶約束,帶默認值的路由規則

函數簽名:MapRoute( string name, string url, object defaults, object constraints, string[] namespaces);

1 routes.MapRoute(
2                  "Rule1",
3                  "Admin/{controller}/{action}-{Year}-{Month}-{Day}",
4                  new { controller = "Home", action = "Index", Year = "2010", Month = "04", Day = "21" },
5                  new { Year = @"^\d{4}", Month = @"\d{2}" },
6                  new string[] { "MvcDemo.Controllers" }
7 );

Url:http://localhost:14039/Admin/home/index-2010-01-21

實例六:捕獲全部的路由

1 routes.MapRoute(
2                  "All", // 路由名稱
3                 "{*Vauler}", // 帶有參數的 URL
4                  new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 參數默認值
5  );
1 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");//是忽略這個規則的Url
1 AreaRegistration.RegisterAllAreas();//註冊全部的Areas
2 RegisterRoutes(RouteTable.Routes);//註冊自定義規則
3 
4 ////調試用語句,須要下載RouteDebug.dll,並添加引用!加入這句話後就能夠測試Url路由了。
5 //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
相關文章
相關標籤/搜索