ASP.NET MVC三個重要的描述對象:ControllerDescriptor
ASP.NET MVC應用的請求都是針對某個Controller的某個Action方法,所以對請求的處理最終體現在對目標Action方法的執行。而Action方法具有相應的參數,所以在方法執行之前必須根據相應的規則從請求中提取相應的數據并將其轉換為Action方法參數列表,我們將這個過程稱為Model綁定。在ASP.NET MVC應用編程接口中,Action方法某個參數的元數據通過ParameterDescriptor表示,而兩個相關的類型ControllerDescriptor和ActionDescriptor則用于描述Controller和Action方法。[本文已經同步到《How ASP.NET MVC Works?》中]
一、ControllerDescriptor
ControllerDescriptor包含了用于描述某個Controller的元數據信息。如下面的代碼片斷所示,ControllerDescriptor具有三個屬性,其中ControllerName和ControllerType分別表示Controller的名稱和類型,前者來源于路由信息;字符串類型的UniqueId表示ControllerDescriptor的唯一標識,該標識由自身的類型、Controller的類型以及Controller的名稱三者派生。
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數組。
二、ReflectedControllerDescriptor
在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數組,僅限于公有實例方法,但是從.Controller中繼承下來的方法除外。當我們調用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,而最終會導致一個狀態碼為404的HttpException異常的拋出;如果具有多個匹配的Action方法,則直接拋出AmbiguousMatchException異常。也就是說對于每一次請求,要求有且只有一個匹配的Action方法。
三、ReflectedAsyncControllerDescriptor
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方法由兩個匹配的方法({ActionName}Async和{ActionName}Completed)構成,ReflectedAsyncControllerDescriptor在根據指定的Action名稱對方法成員進行匹配的時候會自動忽略掉方法名稱的“Async”和“Completed”后綴。
ASP.NET MVC三個重要的描述對象:ControllerDescriptor
ASP.NET MVC三個重要的描述對象:ActionDescriptor
ASP.NET MVC三個重要的描述對象:ControllerDescriptor與ActionDescriptor的創建機制
ASP.NET MVC三個重要的描述對象:ParameterDescriptor


ASP.NET MVC應用的請求都是針對某個Controller的某個Action方法,所以對請求的處理最終體現在對目標Action方法的執行。而Action方法具有相應的參數,所以在方法執行之前必須根據相應的規則從請求中提取相應的數據并將其轉換為Action方法參數列表,我們將這個過程稱為Model綁定。在ASP.NET MVC應用編程接口中,Action方法某個參數的元數據通過ParameterDescriptor表示,而兩個相關的類型ControllerDescriptor和ActionDescriptor則用于描述Controller和Action方法。
浙公網安備 33010602011771號