閱讀874 返回首頁    go 技術社區[雲棲]


ASP.NET MVC三個重要的描述對象:ControllerDescriptor

ASP.NET MVC應用的請求都是針對某個Controller的某個Action方法,所以對請求的處理最終體現在對目標Action方法的執行。而Action方法具有相應的參數,所以在方法執行之前必須根據相應的規則從請求中提取相應的數據並將其轉換為Action方法參數列表,我們將這個過程稱為Model綁定。在ASP.NET MVC應用編程接口中,Action方法某個參數的元數據通過ParameterDescriptor表示,而兩個相關的類型ControllerDescriptorActionDescriptor則用於描述Controller和Action方法。[本文已經同步到《How ASP.NET MVC Works?》中]

ControllerDescriptor包含了用於描述某個Controller的元數據信息。如下麵的代碼片斷所示,ControllerDescriptor具有三個屬性,其中ControllerName和ControllerType分別表示Controller的名稱和類型,前者來源於路由信息;字符串類型的UniqueId表示ControllerDescriptor的唯一標識,該標識由、以及三者派生。

   1: public abstract class ControllerDescriptor : ICustomAttributeProvider
   2: {   
   3:      public virtual object[] GetCustomAttributes(bool inherit);
   4:      public virtual object[] GetCustomAttributes(Type attributeType, bool inherit);
   5:      public virtual bool IsDefined(Type attributeType, bool inherit);
   6:      public virtual IEnumerable<FilterAttribute> GetFilterAttributes(bool useCache);
   7:  
   8:      public abstract ActionDescriptor FindAction(ControllerContext controllerContext, string actionName);
   9:      public abstract ActionDescriptor[] GetCanonicalActions();
  10:   
  11:     public virtual string ControllerName { get; }
  12:     public abstract Type ControllerType { get; }
  13:     public virtual string UniqueId { get; }
  14: }
  15:  
  16: public interface ICustomAttributeProvider
  17: {
  18:     object[] GetCustomAttributes(bool inherit);
  19:     object[] GetCustomAttributes(Type attributeType, bool inherit);
  20:     bool IsDefined(Type attributeType, bool inherit);
  21: }

ControllerDescriptor實現了ICustomAttributeProvider接口,意味著我們可以通過調用GetCustomAttributes和GetCustomAttributes方法獲取應用在Controller類型上的所有自定義特性或者給定類型的特性,也可以調用IsDefined方法判斷指定的自定義特性類型是否應用在對應的Controller類型上。

另一個方法GetFilterAttributes用於獲取應用在Controller上的所有篩選器特性(繼承自抽象類FilterAttribute)。篩選器是一種基於AOP的設計,它使我們可以一些基於橫切關注點相關邏輯的執行動態的注入到Action方法的執行前後,我們會在“Action方法的執行”中對篩選器進行詳細地介紹。

ControllerDescriptor的FindAction方法根據指定的Controller上下文和名稱得到相應的Action方法,返回的是用於描述Action方法的ActionDescriptor對象。而GetCanonicalActions得到當前Controller的所有Action方法,返回類型為ActionDescriptor數組。

在ASP.NET MVC應用編程接口中定義了抽象類ControllerDescriptor的唯一繼承類型ReflectedControllerDescriptor。顧名思義,ReflectedControllerDescriptor通過反射的機製解析用於描述Controller的元數據。如下麵的代碼片斷所示,表示Controller類型的ControllerType屬性在構造函數中指定。ReflectedControllerDescriptor通過反射的方式獲取應用在Controller類型上的相關特性以提供針對ICustomAttributeProvider接口的實現。

   1: public class ReflectedControllerDescriptor : ControllerDescriptor
   2: {
   3:     public ReflectedControllerDescriptor(Type controllerType);
   4:    
   5:     public override object[] GetCustomAttributes(bool inherit);
   6:     public override object[] GetCustomAttributes(Type attributeType, bool inherit);
   7:     public override IEnumerable<FilterAttribute> GetFilterAttributes(bool useCache);
   8:     public override bool IsDefined(Type attributeType, bool inherit);
   9:  
  10:     public override ActionDescriptor FindAction( ControllerContext controllerContext, string actionName);
  11:     public override ActionDescriptor[] GetCanonicalActions();
  12:  
  13:     public sealed override Type ControllerType { get; }
  14: }

對於GetCanonicalActions方法返回的用於描述所有Action方法的ActionDescriptor數組,僅限於。當我們調用FindAction方法根據Action名稱獲取對應ActionDescriptor的時候,在默情況下會將方法名稱視為Action名稱進行匹配。如果方法上應用了具有如下定義的ActionNameSelectorAttribute特性,會傳入相應的參數調用其IsValidName方法,如果該返回值為True,目標方法會被認為是匹配的Action方法。

   1: [AttributeUsage(AttributeTargets.Method, AllowMultiple = false,  Inherited = true)]
   2: public abstract class ActionNameSelectorAttribute : Attribute
   3: {
   4:     public abstract bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo);
   5: }

顧名思義,抽象類ActionNameSelectorAttribute是一個用於輔助選擇目標Action方法的特性,在ASP.NET MVC應用編程接口中具有一個類型為ActionNameAttribute的繼承者。ActionNameAttribute特性應用於Action方法通過參數值指定一個Action別名,在實現的IsValidName方法中會比較指定的別名是否和當前的Action名稱相匹配。如果具有不同 的Action選擇規則,我們也可以通過自定義ActionNameSelectorAttribute特性的方式來實現。

   1: [AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=true)]
   2: public sealed class ActionNameAttribute : ActionNameSelectorAttribute
   3: {    
   4:     public ActionNameAttribute(string name);
   5:     public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo);
   6:     public string Name { get; }
   7: }

對於FindAction方法,如果找不到與指定Action名稱的Action方法,則返回Null,而最終會導致一個狀態碼為的HttpException異常的拋出;如果具有多個匹配的Action方法,則直接拋出AmbiguousMatchException異常。也就是說。

ReflectedAsyncControllerDescriptor類型為ReflectedControllerDescriptor的異步版本。如下麵的代碼片斷所示,ReflectedAsyncControllerDescriptor和ReflectedControllerDescriptor具有類似的成員定義,實際上除了FindAction和GetCanonicalActions兩個方法,其他方法的實現邏輯(即對應用在Controller類型上的相關特性的解析)與ReflectedControllerDescriptor完全一致。

   1: public class ReflectedAsyncControllerDescriptor : ControllerDescriptor
   2: {  
   3:     public ReflectedAsyncControllerDescriptor(Type controllerType);
   4:     
   5:     public override object[] GetCustomAttributes(bool inherit);
   6:     public override object[] GetCustomAttributes(Type attributeType,  bool inherit);
   7:     public override IEnumerable<FilterAttribute> GetFilterAttributes( bool useCache);
   8:     public override bool IsDefined(Type attributeType, bool inherit);
   9:  
  10:     public override ActionDescriptor FindAction( ControllerContext controllerContext, string actionName);
  11:     public override ActionDescriptor[] GetCanonicalActions();
  12:   
  13:     public sealed override Type ControllerType { get; }
  14: }

ReflectedAsyncControllerDescriptor的GetCanonicalActions總是返回一個空的ActionDescriptor數組。對於繼承自AsyncController的Controller類型,一個異步Action方法由兩個匹配的方法()構成,ReflectedAsyncControllerDescriptor在根據指定的Action名稱對方法成員進行匹配的時候會自動忽略掉方法名稱的“Async”和“Completed”後綴。

ASP.NET MVC三個重要的描述對象:ControllerDescriptor
ASP.NET MVC三個重要的描述對象:ActionDescriptor
ASP.NET MVC三個重要的描述對象:ControllerDescriptor與ActionDescriptor的創建機製
ASP.NET MVC三個重要的描述對象:ParameterDescriptor


作者:蔣金楠
微信公眾賬號:大內老A
微博:www.weibo.com/artech
如果你想及時得到個人撰寫文章以及著作的消息推送,或者想看看個人推薦的技術資料,可以掃描左邊二維碼(或者長按識別二維碼)關注個人公眾號(原來公眾帳號蔣金楠的自媒體將會停用)。
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁麵明顯位置給出原文連接,否則保留追究法律責任的權利。
原文鏈接

最後更新:2017-10-26 11:34:36

  上一篇:go  獨家 | 一文讀懂集成學習(附學習資源)
  下一篇:go  ASP.NET MVC三個重要的描述對象:ActionDescriptor