ASP. net mvc 1.0 Process Analysis (ControllerActionInvoker)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.