現在我們首先來看看ActionInvoker屬性的定義吧:
復制代碼 代碼如下:
public IActionInvoker ActionInvoker {
get {
if (_actionInvoker == null) {
_actionInvoker = CreateActionInvoker();
}
return _actionInvoker;
}
set {
_actionInvoker = value;
}
}
protected virtual IActionInvoker CreateActionInvoker() {
return new ControllerActionInvoker();
}
和TempDataProvider屬性定義一樣,大家一定要習慣這些代碼啊。
而ControllerActionInvoker的定義也很簡單,但是這個類卻不簡單啊。
讓我們來看看你InvokeAction的定義吧:
復制代碼 代碼如下:
public virtual bool InvokeAction(ControllerContext controllerContext, string actionName) {
if (controllerContext == null) {
throw new ArgumentNullException("controllerContext");
}
if (String.IsNullOrEmpty(actionName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
}
ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext);
ActionDescriptor actionDescriptor = FindAction(controllerContext, controllerDescriptor, actionName);
if (actionDescriptor != null) {
FilterInfo filterInfo = GetFilters(controllerContext, actionDescriptor);
try {
AuthorizationContext authContext = InvokeAuthorizationFilters(controllerContext, filterInfo.AuthorizationFilters, actionDescriptor);
if (authContext.Result != null) {
// the auth filter signaled that we should let it short-circuit the request
InvokeActionResult(controllerContext, authContext.Result);
}
else {
if (controllerContext.Controller.ValidateRequest) {
ValidateRequest(controllerContext);
}
IDictionarystring, object> parameters = GetParameterValues(controllerContext, actionDescriptor);
ActionExecutedContext postActionContext = InvokeActionMethodWithFilters(controllerContext, filterInfo.ActionFilters, actionDescriptor, parameters);
InvokeActionResultWithFilters(controllerContext, filterInfo.ResultFilters, postActionContext.Result);
}
}
catch (ThreadAbortException) {
// This type of exception occurs as a result of Response.Redirect(), but we special-case so that
// the filters don't see this as an error.
throw;
}
catch (Exception ex) {
// something blew up, so execute the exception filters
ExceptionContext exceptionContext = InvokeExceptionFilters(controllerContext, filterInfo.ExceptionFilters, ex);
if (!exceptionContext.ExceptionHandled) {
throw;
}
InvokeActionResult(controllerContext, exceptionContext.Result);
}
return true;
}
// notify controller that no method matched
return false;
}
這個方法里面的內容不可能一次講完的,我們看看 ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext);
很明顯ControllerDescriptor是Controller實例的一個包裝類。
復制代碼 代碼如下:
protected virtual ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext) {
Type controllerType = controllerContext.Controller.GetType();
ControllerDescriptor controllerDescriptor = DescriptorCache.GetDescriptor(controllerType, () => new ReflectedControllerDescriptor(controllerType));
return controllerDescriptor;
}
從這個方法中,我們可以知道實際返回的是一個ReflectedControllerDescriptor實例,它是ControllerDescriptor的子類,DescriptorCache.GetDescriptor(...)好像是從緩存中獲取的啊,讓我們證實一下吧,先來看看ControllerDescriptorCache的GetDescriptor方法:
復制代碼 代碼如下:
internal sealed class ControllerDescriptorCache : ReaderWriterCacheType, ControllerDescriptor> {
public ControllerDescriptor GetDescriptor(Type controllerType, FuncControllerDescriptor> creator) {
return FetchOrCreateItem(controllerType, creator);
}
}
FetchOrCreateItem方法很簡單,從緩存中獲取ControllerDescriptor ,如果沒有就創建并加入緩存然后在返回,緩存實現方式其實就是一個字典DictionaryTKey, TValue>。
現在看看ReflectedControllerDescriptor的夠著函數是否有什么特別之處:
_controllerType = controllerType;
_selector = new ActionMethodSelector(_controllerType);
怎么又有ActionMethodSelector這個東東啊,其構造函數如下
復制代碼 代碼如下:
public ActionMethodSelector(Type controllerType) {
ControllerType = controllerType;
PopulateLookupTables();
}
private void PopulateLookupTables() {
MethodInfo[] allMethods = ControllerType.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public);
MethodInfo[] actionMethods = Array.FindAll(allMethods, IsValidActionMethod);
AliasedMethods = Array.FindAll(actionMethods, IsMethodDecoratedWithAliasingAttribute);
NonAliasedMethods = actionMethods.Except(AliasedMethods).ToLookup(method => method.Name, StringComparer.OrdinalIgnoreCase);
}
這個方法很簡單,找出ControllerType的所有實例、共有方法,然后在過濾調不是Action的,最后吧這些Action方法分成兩部分,一部分有別名,一部分沒有別名。
現在我們已經得到了ControllerDescriptor實例,下面應該來看看ActionDescriptor actionDescriptor = FindAction(controllerContext, controllerDescriptor, actionName);這句代碼了;同樣我們可以確認ActionDescriptor實際上一個Action的包裝類。
protected virtual ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)這個方法實際上就是調用
ControllerDescriptor類FindAction方法,讓我們看看你ReflectedControllerDescriptor的FindAction方法,該方法很簡單,組要就2句代碼:
復制代碼 代碼如下:
MethodInfo matched = _selector.FindActionMethod(controllerContext, actionName);
return new ReflectedActionDescriptor(matched, actionName, this);
_selector.FindActionMethod(controllerContext, actionName); 這句就是找到我們需要Action對應的MethodInfo。
復制代碼 代碼如下:
public MethodInfo FindActionMethod(ControllerContext controllerContext, string actionName) {
ListMethodInfo> methodsMatchingName = GetMatchingAliasedMethods(controllerContext, actionName);
methodsMatchingName.AddRange(NonAliasedMethods[actionName]);
ListMethodInfo> finalMethods = RunSelectionFilters(controllerContext, methodsMatchingName);
switch (finalMethods.Count) {
case 0:
return null;
case 1:
return finalMethods[0];
default:
throw CreateAmbiguousMatchException(finalMethods, actionName);
}
}
循環每個MethodInfo,查找它們的自定義的ActionMethodSelectorAttribute特性,如果有只返回驗證通過的特性。看到ReflectedAttributeCache.GetActionMethodSelectorAttributes(methodInfo)這樣的代碼感覺由于緩存有關,
復制代碼 代碼如下:
private static ReadOnlyCollectionTAttribute> GetAttributesTMemberInfo, TAttribute>(ConcurrentDictionaryTMemberInfo, ReadOnlyCollectionTAttribute>> lookup, TMemberInfo memberInfo)
where TAttribute : Attribute
where TMemberInfo : MemberInfo {
return lookup.GetOrAdd(memberInfo, mi => new ReadOnlyCollectionTAttribute>((TAttribute[])memberInfo.GetCustomAttributes(typeof(TAttribute), inherit: true)));
}
ReflectedAttributeCache這個類有幾個緩存字典:
ConcurrentDictionaryMethodInfo, ReadOnlyCollectionActionMethodSelectorAttribute>>
ConcurrentDictionaryMethodInfo, ReadOnlyCollectionActionNameSelectorAttribute>>
ConcurrentDictionaryMethodInfo, ReadOnlyCollectionFilterAttribute>>
ConcurrentDictionaryType, ReadOnlyCollectionFilterAttribute>>
默認實現ActionMethodSelectorAttribute類主要有以下幾個
AcceptVerbsAttribute
HttpDeleteAttribute
HttpGetAttribute
HttpPostAttribute
HttpPutAttribute
NonActionAttribute
AcceptVerbsAttribute
剩下的就是直接實例一個ReflectedActionDescriptor對象了,這個也沒什么特殊,只是里面有一個驗證方法
復制代碼 代碼如下:
if (validateMethod) {
string failedMessage = VerifyActionMethodIsCallable(methodInfo);
if (failedMessage != null) {
throw new ArgumentException(failedMessage, "methodInfo");
}
}
用來驗證該方法是否可以執行,以下幾種情況經不會通過,(1)方法是靜態方法(2)方法的實例類型不是ControllerBase(3)是否包含泛型參數如 public ActionResult IndexT>()是非法的,但 public ActionResult Index(Liststring> aa)是合法(4)參數中不能含有Ref和out。
這篇文章說的很散,我們需要注意一點微軟在mvc里面緩存做的很好了,在前面個將獲取ControllerTyper時它是有緩存的,一次讀取當前程序集所有的ControllerType,在這里提到了一個DescriptorCache 緩存每次調用的ControllerType->ReflectedControllerDescriptor,而ReflectedControllerDescriptor實例會一次去讀該Controller的所有Action方法;這里還有一個ReflectedAttributeCache,緩存每次調用MethodInfo的所有特性(ActionMethodSelectorAttribute、ActionNameSelectorAttribute、FilterAttribute),當然FilterAttribute特性還可以在類上面。
您可能感興趣的文章:- ASP.NET MVC中URL地址傳參的兩種寫法
- 解讀ASP.NET 5 & MVC6系列教程(10):Controller與Action
- 詳解ASP.NET MVC下的異步Action的定義和執行原理
- ASP.NET MVC使用ActionFilterAttribute實現權限限制的方法(附demo源碼下載)
- asp.net MVC利用ActionFilterAttribute過濾關鍵字的方法
- 使用ASP.NET MVC 4 Async Action+jQuery實現消息通知機制的實現代碼
- ASP.NET MVC:Filter和Action的執行介紹
- asp.net MVC實現無組件上傳圖片實例介紹
- ASP.NET MVC DropDownList數據綁定及使用詳解
- ASP.NET MVC 控制器與視圖
- ASP.NET實現MVC中獲取當前URL、controller及action的方法