The ControllerActionInvoker type code is still commendable. The core call code is simple and clear, and the related decomposition methods are clear and clean, making it easy to understand this complicated execution process.
Public class ControllerActionInvoker: IActionInvoker
{
Public virtual bool InvokeAction (ControllerContext controllerContext, string 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 shocould let it short-circuit the request
InvokeActionResult (controllerContext, authContext. Result );
}
Else
{
If (controllerContext. Controller. ValidateRequest)
{
ValidateRequest (controllerContext. HttpContext. Request );
}
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;
}
// Policy controller that no method matched
Return false;
}
}
1. GetControllerDescriptor: first, get ControllerDescriptor. By default, ReflectedControllerDescriptor is returned.
Public class ControllerActionInvoker: IActionInvoker
{
Protected virtual ControllerDescriptor GetControllerDescriptor (ControllerContext controllerContext)
{
Type controllerType = controllerContext. Controller. GetType ();
ControllerDescriptor controllerDescriptor = DescriptorCache. GetDescriptor (controllerType );
Return controllerDescriptor;
}
Private readonly static ControllerDescriptorCache _ staticDescriptorCache = new ControllerDescriptorCache ();
Internal ControllerDescriptorCache DescriptorCache
{
Get
{
If (_ instanceDescriptorCache = null)
{
_ InstanceDescriptorCache = _ staticDescriptorCache;
}
Return _ instanceDescriptorCache;
}
...
}
}
Internal sealed class ControllerDescriptorCache: ReaderWriterCache <Type, ControllerDescriptor>
{
Public ControllerDescriptor GetDescriptor (Type controllerType)
{
Return FetchOrCreateItem (controllerType, () => new ReflectedControllerDescriptor (controllerType ));
}
}
2. FindAction: Find the actual execution Action information and return ReflectedActionDescriptor.
Public class ControllerActionInvoker: IActionInvoker
{
Protected virtual ActionDescriptor FindAction (ControllerContext controllerContext ,...)
{
ActionDescriptor actionDescriptor = controllerDescriptor. FindAction (controllerContext, actionName );
Return actionDescriptor;
}
}
Public class ReflectedControllerDescriptor: ControllerDescriptor
{
Public ReflectedControllerDescriptor (Type controllerType)
{
...
_ Selector = new ActionMethodSelector (_ controllerType );
}
Public override ActionDescriptor FindAction (ControllerContext controllerContext, string actionName)
{
...
MethodInfo matched = _ selector. FindActionMethod (controllerContext, actionName );
If (matched = null)
{
Return null;
}
Return new ReflectedActionDescriptor (matched, actionName, this );
}
}
Continue to go in and see how _ selector. FindActionMethod () returns the actual Action MethodInfo.
Internal sealed class ActionMethodSelector
{
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 );
}
}
}
- 11 pages in total:
- Previous Page
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- Next Page