asp.net mvc-Controllerl篇 ControllerDescriptor
更新時(shí)間:2012年11月09日 16:50:52 作者:
asp.net mvc-Controllerl篇 ControllerDescriptor
現(xiàn)在我們首先來(lái)看看ActionInvoker屬性的定義吧:
public IActionInvoker ActionInvoker {
get {
if (_actionInvoker == null) {
_actionInvoker = CreateActionInvoker();
}
return _actionInvoker;
}
set {
_actionInvoker = value;
}
}
protected virtual IActionInvoker CreateActionInvoker() {
return new ControllerActionInvoker();
}
和TempDataProvider屬性定義一樣,大家一定要習(xí)慣這些代碼啊。
而ControllerActionInvoker的定義也很簡(jiǎn)單,但是這個(gè)類(lèi)卻不簡(jiǎn)單啊。
讓我們來(lái)看看你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);
}
IDictionary<string, 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;
}
這個(gè)方法里面的內(nèi)容不可能一次講完的,我們看看 ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext);
很明顯ControllerDescriptor是Controller實(shí)例的一個(gè)包裝類(lèi)。
protected virtual ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext) {
Type controllerType = controllerContext.Controller.GetType();
ControllerDescriptor controllerDescriptor = DescriptorCache.GetDescriptor(controllerType, () => new ReflectedControllerDescriptor(controllerType));
return controllerDescriptor;
}
從這個(gè)方法中,我們可以知道實(shí)際返回的是一個(gè)ReflectedControllerDescriptor實(shí)例,它是ControllerDescriptor的子類(lèi),DescriptorCache.GetDescriptor(...)好像是從緩存中獲取的啊,讓我們證實(shí)一下吧,先來(lái)看看ControllerDescriptorCache的GetDescriptor方法:
internal sealed class ControllerDescriptorCache : ReaderWriterCache<Type, ControllerDescriptor> {
public ControllerDescriptor GetDescriptor(Type controllerType, Func<ControllerDescriptor> creator) {
return FetchOrCreateItem(controllerType, creator);
}
}
FetchOrCreateItem方法很簡(jiǎn)單,從緩存中獲取ControllerDescriptor ,如果沒(méi)有就創(chuàng)建并加入緩存然后在返回,緩存實(shí)現(xiàn)方式其實(shí)就是一個(gè)字典Dictionary<TKey, TValue>。
現(xiàn)在看看ReflectedControllerDescriptor的夠著函數(shù)是否有什么特別之處:
_controllerType = controllerType;
_selector = new ActionMethodSelector(_controllerType);
怎么又有ActionMethodSelector這個(gè)東東啊,其構(gòu)造函數(shù)如下
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);
}
這個(gè)方法很簡(jiǎn)單,找出ControllerType的所有實(shí)例、共有方法,然后在過(guò)濾調(diào)不是Action的,最后吧這些Action方法分成兩部分,一部分有別名,一部分沒(méi)有別名。
現(xiàn)在我們已經(jīng)得到了ControllerDescriptor實(shí)例,下面應(yīng)該來(lái)看看ActionDescriptor actionDescriptor = FindAction(controllerContext, controllerDescriptor, actionName);這句代碼了;同樣我們可以確認(rèn)ActionDescriptor實(shí)際上一個(gè)Action的包裝類(lèi)。
protected virtual ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)這個(gè)方法實(shí)際上就是調(diào)用
ControllerDescriptor類(lèi)FindAction方法,讓我們看看你ReflectedControllerDescriptor的FindAction方法,該方法很簡(jiǎn)單,組要就2句代碼:
MethodInfo matched = _selector.FindActionMethod(controllerContext, actionName);
return new ReflectedActionDescriptor(matched, actionName, this);
_selector.FindActionMethod(controllerContext, actionName); 這句就是找到我們需要Action對(duì)應(yīng)的MethodInfo。
public MethodInfo FindActionMethod(ControllerContext controllerContext, string actionName) {
List<MethodInfo> methodsMatchingName = GetMatchingAliasedMethods(controllerContext, actionName);
methodsMatchingName.AddRange(NonAliasedMethods[actionName]);
List<MethodInfo> finalMethods = RunSelectionFilters(controllerContext, methodsMatchingName);
switch (finalMethods.Count) {
case 0:
return null;
case 1:
return finalMethods[0];
default:
throw CreateAmbiguousMatchException(finalMethods, actionName);
}
}
循環(huán)每個(gè)MethodInfo,查找它們的自定義的ActionMethodSelectorAttribute特性,如果有只返回驗(yàn)證通過(guò)的特性??吹絉eflectedAttributeCache.GetActionMethodSelectorAttributes(methodInfo)這樣的代碼感覺(jué)由于緩存有關(guān),
private static ReadOnlyCollection<TAttribute> GetAttributes<TMemberInfo, TAttribute>(ConcurrentDictionary<TMemberInfo, ReadOnlyCollection<TAttribute>> lookup, TMemberInfo memberInfo)
where TAttribute : Attribute
where TMemberInfo : MemberInfo {
return lookup.GetOrAdd(memberInfo, mi => new ReadOnlyCollection<TAttribute>((TAttribute[])memberInfo.GetCustomAttributes(typeof(TAttribute), inherit: true)));
}
ReflectedAttributeCache這個(gè)類(lèi)有幾個(gè)緩存字典:
ConcurrentDictionary<MethodInfo, ReadOnlyCollection<ActionMethodSelectorAttribute>>
ConcurrentDictionary<MethodInfo, ReadOnlyCollection<ActionNameSelectorAttribute>>
ConcurrentDictionary<MethodInfo, ReadOnlyCollection<FilterAttribute>>
ConcurrentDictionary<Type, ReadOnlyCollection<FilterAttribute>>
默認(rèn)實(shí)現(xiàn)ActionMethodSelectorAttribute類(lèi)主要有以下幾個(gè)
AcceptVerbsAttribute
HttpDeleteAttribute
HttpGetAttribute
HttpPostAttribute
HttpPutAttribute
NonActionAttribute
AcceptVerbsAttribute
剩下的就是直接實(shí)例一個(gè)ReflectedActionDescriptor對(duì)象了,這個(gè)也沒(méi)什么特殊,只是里面有一個(gè)驗(yàn)證方法
if (validateMethod) {
string failedMessage = VerifyActionMethodIsCallable(methodInfo);
if (failedMessage != null) {
throw new ArgumentException(failedMessage, "methodInfo");
}
}
用來(lái)驗(yàn)證該方法是否可以執(zhí)行,以下幾種情況經(jīng)不會(huì)通過(guò),(1)方法是靜態(tài)方法(2)方法的實(shí)例類(lèi)型不是ControllerBase(3)是否包含泛型參數(shù)如 public ActionResult Index<T>()是非法的,但 public ActionResult Index(List<string> aa)是合法(4)參數(shù)中不能含有Ref和out。
這篇文章說(shuō)的很散,我們需要注意一點(diǎn)微軟在mvc里面緩存做的很好了,在前面?zhèn)€將獲取ControllerTyper時(shí)它是有緩存的,一次讀取當(dāng)前程序集所有的ControllerType,在這里提到了一個(gè)DescriptorCache 緩存每次調(diào)用的ControllerType->ReflectedControllerDescriptor,而ReflectedControllerDescriptor實(shí)例會(huì)一次去讀該Controller的所有Action方法;這里還有一個(gè)ReflectedAttributeCache,緩存每次調(diào)用MethodInfo的所有特性(ActionMethodSelectorAttribute、ActionNameSelectorAttribute、FilterAttribute),當(dāng)然FilterAttribute特性還可以在類(lèi)上面。
復(fù)制代碼 代碼如下:
public IActionInvoker ActionInvoker {
get {
if (_actionInvoker == null) {
_actionInvoker = CreateActionInvoker();
}
return _actionInvoker;
}
set {
_actionInvoker = value;
}
}
protected virtual IActionInvoker CreateActionInvoker() {
return new ControllerActionInvoker();
}
和TempDataProvider屬性定義一樣,大家一定要習(xí)慣這些代碼啊。
而ControllerActionInvoker的定義也很簡(jiǎn)單,但是這個(gè)類(lèi)卻不簡(jiǎn)單啊。
讓我們來(lái)看看你InvokeAction的定義吧:
復(fù)制代碼 代碼如下:
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);
}
IDictionary<string, 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;
}
這個(gè)方法里面的內(nèi)容不可能一次講完的,我們看看 ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext);
很明顯ControllerDescriptor是Controller實(shí)例的一個(gè)包裝類(lèi)。
復(fù)制代碼 代碼如下:
protected virtual ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext) {
Type controllerType = controllerContext.Controller.GetType();
ControllerDescriptor controllerDescriptor = DescriptorCache.GetDescriptor(controllerType, () => new ReflectedControllerDescriptor(controllerType));
return controllerDescriptor;
}
從這個(gè)方法中,我們可以知道實(shí)際返回的是一個(gè)ReflectedControllerDescriptor實(shí)例,它是ControllerDescriptor的子類(lèi),DescriptorCache.GetDescriptor(...)好像是從緩存中獲取的啊,讓我們證實(shí)一下吧,先來(lái)看看ControllerDescriptorCache的GetDescriptor方法:
復(fù)制代碼 代碼如下:
internal sealed class ControllerDescriptorCache : ReaderWriterCache<Type, ControllerDescriptor> {
public ControllerDescriptor GetDescriptor(Type controllerType, Func<ControllerDescriptor> creator) {
return FetchOrCreateItem(controllerType, creator);
}
}
FetchOrCreateItem方法很簡(jiǎn)單,從緩存中獲取ControllerDescriptor ,如果沒(méi)有就創(chuàng)建并加入緩存然后在返回,緩存實(shí)現(xiàn)方式其實(shí)就是一個(gè)字典Dictionary<TKey, TValue>。
現(xiàn)在看看ReflectedControllerDescriptor的夠著函數(shù)是否有什么特別之處:
_controllerType = controllerType;
_selector = new ActionMethodSelector(_controllerType);
怎么又有ActionMethodSelector這個(gè)東東啊,其構(gòu)造函數(shù)如下
復(fù)制代碼 代碼如下:
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);
}
這個(gè)方法很簡(jiǎn)單,找出ControllerType的所有實(shí)例、共有方法,然后在過(guò)濾調(diào)不是Action的,最后吧這些Action方法分成兩部分,一部分有別名,一部分沒(méi)有別名。
現(xiàn)在我們已經(jīng)得到了ControllerDescriptor實(shí)例,下面應(yīng)該來(lái)看看ActionDescriptor actionDescriptor = FindAction(controllerContext, controllerDescriptor, actionName);這句代碼了;同樣我們可以確認(rèn)ActionDescriptor實(shí)際上一個(gè)Action的包裝類(lèi)。
protected virtual ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)這個(gè)方法實(shí)際上就是調(diào)用
ControllerDescriptor類(lèi)FindAction方法,讓我們看看你ReflectedControllerDescriptor的FindAction方法,該方法很簡(jiǎn)單,組要就2句代碼:
復(fù)制代碼 代碼如下:
MethodInfo matched = _selector.FindActionMethod(controllerContext, actionName);
return new ReflectedActionDescriptor(matched, actionName, this);
_selector.FindActionMethod(controllerContext, actionName); 這句就是找到我們需要Action對(duì)應(yīng)的MethodInfo。
復(fù)制代碼 代碼如下:
public MethodInfo FindActionMethod(ControllerContext controllerContext, string actionName) {
List<MethodInfo> methodsMatchingName = GetMatchingAliasedMethods(controllerContext, actionName);
methodsMatchingName.AddRange(NonAliasedMethods[actionName]);
List<MethodInfo> finalMethods = RunSelectionFilters(controllerContext, methodsMatchingName);
switch (finalMethods.Count) {
case 0:
return null;
case 1:
return finalMethods[0];
default:
throw CreateAmbiguousMatchException(finalMethods, actionName);
}
}
循環(huán)每個(gè)MethodInfo,查找它們的自定義的ActionMethodSelectorAttribute特性,如果有只返回驗(yàn)證通過(guò)的特性??吹絉eflectedAttributeCache.GetActionMethodSelectorAttributes(methodInfo)這樣的代碼感覺(jué)由于緩存有關(guān),
復(fù)制代碼 代碼如下:
private static ReadOnlyCollection<TAttribute> GetAttributes<TMemberInfo, TAttribute>(ConcurrentDictionary<TMemberInfo, ReadOnlyCollection<TAttribute>> lookup, TMemberInfo memberInfo)
where TAttribute : Attribute
where TMemberInfo : MemberInfo {
return lookup.GetOrAdd(memberInfo, mi => new ReadOnlyCollection<TAttribute>((TAttribute[])memberInfo.GetCustomAttributes(typeof(TAttribute), inherit: true)));
}
ReflectedAttributeCache這個(gè)類(lèi)有幾個(gè)緩存字典:
ConcurrentDictionary<MethodInfo, ReadOnlyCollection<ActionMethodSelectorAttribute>>
ConcurrentDictionary<MethodInfo, ReadOnlyCollection<ActionNameSelectorAttribute>>
ConcurrentDictionary<MethodInfo, ReadOnlyCollection<FilterAttribute>>
ConcurrentDictionary<Type, ReadOnlyCollection<FilterAttribute>>
默認(rèn)實(shí)現(xiàn)ActionMethodSelectorAttribute類(lèi)主要有以下幾個(gè)
AcceptVerbsAttribute
HttpDeleteAttribute
HttpGetAttribute
HttpPostAttribute
HttpPutAttribute
NonActionAttribute
AcceptVerbsAttribute
剩下的就是直接實(shí)例一個(gè)ReflectedActionDescriptor對(duì)象了,這個(gè)也沒(méi)什么特殊,只是里面有一個(gè)驗(yàn)證方法
復(fù)制代碼 代碼如下:
if (validateMethod) {
string failedMessage = VerifyActionMethodIsCallable(methodInfo);
if (failedMessage != null) {
throw new ArgumentException(failedMessage, "methodInfo");
}
}
用來(lái)驗(yàn)證該方法是否可以執(zhí)行,以下幾種情況經(jīng)不會(huì)通過(guò),(1)方法是靜態(tài)方法(2)方法的實(shí)例類(lèi)型不是ControllerBase(3)是否包含泛型參數(shù)如 public ActionResult Index<T>()是非法的,但 public ActionResult Index(List<string> aa)是合法(4)參數(shù)中不能含有Ref和out。
這篇文章說(shuō)的很散,我們需要注意一點(diǎn)微軟在mvc里面緩存做的很好了,在前面?zhèn)€將獲取ControllerTyper時(shí)它是有緩存的,一次讀取當(dāng)前程序集所有的ControllerType,在這里提到了一個(gè)DescriptorCache 緩存每次調(diào)用的ControllerType->ReflectedControllerDescriptor,而ReflectedControllerDescriptor實(shí)例會(huì)一次去讀該Controller的所有Action方法;這里還有一個(gè)ReflectedAttributeCache,緩存每次調(diào)用MethodInfo的所有特性(ActionMethodSelectorAttribute、ActionNameSelectorAttribute、FilterAttribute),當(dāng)然FilterAttribute特性還可以在類(lèi)上面。
您可能感興趣的文章:
- ASP.NET MVC中URL地址傳參的兩種寫(xiě)法
- 解讀ASP.NET 5 & MVC6系列教程(10):Controller與Action
- 詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理
- ASP.NET MVC使用ActionFilterAttribute實(shí)現(xiàn)權(quán)限限制的方法(附demo源碼下載)
- asp.net MVC利用ActionFilterAttribute過(guò)濾關(guān)鍵字的方法
- 使用ASP.NET MVC 4 Async Action+jQuery實(shí)現(xiàn)消息通知機(jī)制的實(shí)現(xiàn)代碼
- ASP.NET MVC:Filter和Action的執(zhí)行介紹
- asp.net MVC實(shí)現(xiàn)無(wú)組件上傳圖片實(shí)例介紹
- ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
- ASP.NET MVC 控制器與視圖
- ASP.NET實(shí)現(xiàn)MVC中獲取當(dāng)前URL、controller及action的方法
相關(guān)文章
C# 文件保存到數(shù)據(jù)庫(kù)中或者從數(shù)據(jù)庫(kù)中讀取文件
在編程中我們常常會(huì)遇到“將文件保存到數(shù)據(jù)庫(kù)中”這樣一個(gè)問(wèn)題,雖然這已不是什么高難度的問(wèn)題,但對(duì)于一些剛剛開(kāi)始編程的朋友來(lái)說(shuō)可能是有一點(diǎn)困難。2009-03-03ASP.NET Core環(huán)境變量配置和啟動(dòng)設(shè)置講解
這篇文章介紹了ASP.NET Core環(huán)境變量配置和啟動(dòng)設(shè)置的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02asp.net在事件中啟動(dòng)線(xiàn)程來(lái)打開(kāi)一個(gè)頁(yè)面的實(shí)現(xiàn)方法
點(diǎn)擊一個(gè)按鈕做兩件事情,一件需要點(diǎn)擊按鈕馬上完成,另一件事情是點(diǎn)擊按鈕后做其他事情,不會(huì)的朋友一起來(lái)看看下面是如何實(shí)現(xiàn)的2014-11-11ASP.NET MVC中將控制器分離到類(lèi)庫(kù)的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET MVC中將控制器分離到類(lèi)庫(kù)的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2015-06-06Asp.Net Core 中的“虛擬目錄”實(shí)現(xiàn)
這篇文章主要介紹了Asp.Net Core 中的“虛擬目錄”實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08使用visual studio自動(dòng)創(chuàng)建IIS虛擬目錄
使用visual studio自動(dòng)創(chuàng)建IIS虛擬目錄,需要的朋友可以參考一下2013-02-02