1、URLRouting簡介
URL(Uniform Resource Locator),統一資源定位器,是用於完整描述Internet上的網頁或其他資源地址的一種標識方法。
URL一般可以由6部分組成,格式如下:
protocol :// hostname [:port] [/path] [?parameters] [#fragment]
URL各部分說明:
- protocol 協議。可以是HTTP(超文字傳輸通訊協定 (HTTP))、FTP(檔案傳輸通訊協定)和HTTPS(安全超文字傳輸通訊協定 (HTTPS))。
- hostname 主機名稱。指在互連網中存放資源的伺服器DNS主機名稱或IP地址。
- port 連接埠號碼。該選項是一個小於66536的正整數,是各伺服器或協議約定的通訊連接埠。
- path 路徑。用來表示一個Web網站中的目錄或檔案資源的地址。
- parameters 參數列表。參數形式為以=隔開的鍵/值對,多個參數之間用&串連。
- fragment 資訊片段。用於直接定位到頁面中的某個錨點標記。
2、URLRouting與URLRewrite區別
URLRouting是一組從URL到請求處理常式間的映射規則,將URL映射到能夠處理業務需求的Action上。URLRouting是一個獨立的類庫System.Web.Routing.dll。
URLRouting為將URL映射到Controller的Action上,處理流程圖如下:
URLRewrite為將URL映射到具體的檔案資源上,處理流程圖如下:
3、ASP.NET MVC中使用及自訂URLRouting規則
ASP.NET MVC項目中,Visual Studio會自動在Global.ascx檔案中定義一個路由規則。URLRouting是為了定義如何處理用戶端的請求,每個ASP.NET MVC應用程式至少需要定義一個URLRouting來指明應用程式如何處理請求,複雜的應用程式可以包含多個URLRouting。
1>、Global.ascx檔案
在ASP.NET MVC中,預設情況下所有的路由規則都被定義在Global.ascx檔案中。新建立的ASP.NET MVC 3.0項目中,Global.ascx檔案代碼如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 using System.Web.Routing;
7
8 namespace Web
9 {
10 // Note: For instructions on enabling IIS6 or IIS7 classic mode,
11 // visit http://go.microsoft.com/?LinkId=9394801
12
13 public class MvcApplication : System.Web.HttpApplication
14 {
15 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
16 {
17 filters.Add(new HandleErrorAttribute());
18 }
19
20 public static void RegisterRoutes(RouteCollection routes)
21 {
22 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
23
24 routes.MapRoute(
25 "Default", // Route name
26 "{controller}/{action}/{id}", // URL with parameters
27 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
28 );
29
30 }
31
32 protected void Application_Start()
33 {
34 AreaRegistration.RegisterAllAreas();
35
36 RegisterGlobalFilters(GlobalFilters.Filters);
37 RegisterRoutes(RouteTable.Routes);
38 }
39 }
40 }
2>、Route類
RouteCollection對象以靜態屬性的方式聲明在RouteTable的屬性Routes中,RouteCollectionObject Storage Service的是Route類的執行個體。一個完整的Route類執行個體需要有URL、預設值、約束、資料密鑰及路由處理常式等屬性。
1 public RouteValueDictionary Constraints { get; set; }
2 public RouteValueDictionary DataTokens { get; set; }
3 public RouteValueDictionary Defaults { get; set; }
4 public IRouteHandler RouteHandler { get; set; }
5 public string Url { get; set; }
3>、Route類屬性
URL屬性
在Route類中,屬性URL是一個字串,用於描述請求中URL的格式。該字串可能不完全是一個實際的URL,可以帶一些{}標記的預留位置,使用預留位置可以從URL中提取資料。如:
1 "{controller}/{action}/{id}"
{controller}參數的值用於執行個體化一個處理請求的控制類對象,{action}參數的值用於指明處理當前請求將調用控制器中的方法。
Defaults屬性
1 new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Constraints屬性
1 new { controller = @"^\w+", action = @"^\w+", id = @"\d+" }
4>、自訂URLRouting規則
a、分頁
routes.MapRoute(
"Page",
"{controller}/List/Page/{page}",
new { controller = "Home", action = "List", page = UrlParameter.Optional },
new { page = @"\d*" }
);
public string List(int? page)
{
return page == null ? "1" : page.ToString();
}
4、使用RouteDebugger調試URLRouting
RouteDebugger為一個獨立的類庫,RouteDebug.dll,可以從網上下載到,使用方法如下:
1>、添加對RouteDebug引用;
2>、Global.ascx修改
1 using RouteDebug;
2
3 protected void Application_Start()
4 {
5 AreaRegistration.RegisterAllAreas();
6
7 RegisterGlobalFilters(GlobalFilters.Filters);
8 RegisterRoutes(RouteTable.Routes);
9
10 RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes); // 添加RouteDebug
11}
附件:RouteDebug.rar