學習ASP.NET MVC5架構揭秘筆記-ASP.NET MVC路由(四),mvc5-asp.net

來源:互聯網
上載者:User

學習ASP.NET MVC5架構揭秘筆記-ASP.NET MVC路由(四),mvc5-asp.net
2.2.4基於Area的路由映射

對於一個較大規模的Web應用,我們可以從功能上通過Area將其劃分為較小的單元。每個Area相當於一個獨立的子系統,它們具有一套包含Models、Views和Controller在內的目錄結構和設定檔。一般來說,每個Area具有各自的路由規則(路由模板上一般會包含Area的名稱),而基於Area的路由映射通過AreaRegistration類型進行註冊。

1.AreaRegistration與AreaRegistrationContext

針對Area的路由通過AreaRegistration來註冊。如下面的代碼所示,AreaRegistration是一個抽象類別,它的抽象唯讀屬性AreaName返回當前Area的名稱,而抽象方法RegisterArea用於實現基於當前Area的路由註冊。

public abstract class AreaRegistration{public static void RegisterAllAreas();public static void RegisterAllAreas(object state);public abstract string AreaName { get; };public abstract void RegisterArea(System.Web.Mvc.AreaRegistrationContext context);}

AreaRegistration定義了兩個抽象的靜態RegisterAllAreas方法重載,參數state表示傳遞給具體AreaRegistration的資料。當RegisterAllArea方法執行的時候,所有被當前Web應用直接或間接引用的程式集會被載入(如果尚未被載入),ASP.NET MVC會從這些程式集中解析出所有繼承自AreaRegistration的類型,並通過反射建立相應的AreaRegistration對象。針對每個被建立出來的AreaRegistration對象,一個作為Area註冊內容相關的AreaRegistrationContext對象會被建立出來,它被作為參數調用這些AreaRegistration對象的RegisterArea方法進行針對相應Area的路由註冊。

如下面的程式碼片段所示,AreaRegistrationContext的唯讀屬性AreaName表示Area的名稱,屬性Routes是一個代表路由表的RouteCollection對象,而State是一個使用者自訂對象,他們均通過建構函式進行初始化。具體來說,AreaRegistrationContext對象是在調用AreaRegistration的靜態方法RegisterAllAreas時針對建立出來的AreaRegistration對象構建的,其AreaName來源於當前AreaRegistration對象的同名屬性,Routes則對應著RouteTable的靜態屬性Routes所表示的全域路由表。調用RegisterAllAreas方法指定的參數state將被作為調用AreaRegistrationContext建構函式的同名參數。

public class AreaRegistrationContext{public AreaRegistrationContext(string areaName,RouteCollection routes, object state);public AreaRegistrationContext(string areaName,RouteCollection routes);public Route MapRoute(string name, string url, object defaults, object constraints, string[] namespaces);public Route MapRoute(string name, string url, object defaults, string[] namespaces);public Route MapRoute(string name, string url, string[] namespaces);public Route MapRoute(string name, string url, object defaults, object constraints);public Route MapRoute(string name, string url, object defaults);public Route MapRoute(string name, string url);public string AreaName { get; }public ICollection<string> Namespaces { get; }public RouteCollection Routes { get; }public object State { get; }}


AreaRegistrationContext的唯讀屬性Namespaces表示一組需要優先匹配的命名空間(當多個同名的Controller類型定義在不同的命名空間的時候,定義在這些命名空間的Controller類型會被優先選用)。當針對某個具體AreaRegistration的AreaRegistrationContext對象被建立的時候,如果AreaRegistration類型定義在某個命名空間(比如“Artech.Controllers”),則在這個命名空間基礎上添加“.*”尾碼產生的字串(比如“Artech.Controllers.*”)會被添加到Namespaces集合中。換言之,對於多個定義在不同命名空間中的同名Controller類型,會優先選擇包含在當前AreaRegistration所在命名空間下的Controller。

AreaRegistrationContext定義了一系列的MapRoute方法進行路由註冊,方法的使用及參數的含義與RouteCollection類的同名擴充方法一致。在這裡需要特別指出的是,如果MapRoute方法沒有指定命名空間,通過屬性Namespaces表示的命名空間列表會被使用;反之,該屬性中包含的命名空間會被直接忽略。

當我們通過Visual Stdio的ASP.NET MVC項目模板建立一個Web應用的時候,在Global.asax檔案中會產生類似如下的代碼,在這裡通過調用AreaRegistration的靜態方法RegisterAllAreas實現對所有Area的註冊。也就是說,針對所有Area的註冊發生在Web應用啟動的時候。

protected void Application_Start(){AreaRegistration.RegisterAllAreas();//其他動作}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.