In-depth understanding of ASP. net mvc (7)

Source: Internet
Author: User

Series directory

Action Positioning

Return to controller'sExecutecoreMethod, return to the action call entry:

 
If (! Actioninvoker. invokeaction (controllercontext, actionname ))

Here, actioninvoker isIactioninvokeIt is undoubtedly responsible For the calling logic of all actions, which is implemented by default in MVCControlleractioninvoke. You can imagineControlleractioninvokeThe first problem is how to find the action corresponding to the actionname.

First, not all actions are valid and must meet the following conditions:

1. Public, non-static

2. Non-controller or its base class defined methods, such as tostring () and gethashcode ()

3. The isspecialname flag of system. reflection. methodbase cannot be a special name, such as constructor, property wrappers, and event wrappers.

4. Although methods with generic parameters are considered action, the framework will only throw an exception when trying to execute such an action.

Secondly, MVC designs two "selectors" for the action search process"

    • Actionnameselectorattribute
    • Actionmethodselectorattribute

If you are confused about this, you may understand their derived features:

 

 

We often useActionnameattributeTo "Disguise"; UseHttppostattributeOrHttpgetattributeTo describe the specific HTTP behavior that an action conforms.NoactionHide the method we don't want to "expose to URL... In fact, the principle of these actions is to use the action selection mechanism provided by MVC. The following figure shows the completion logic of the process:

Contolleractioninvoker. invokeaction createsReflectedcontrollerdescriptorAnd callFindactionThe findaction of this object is responsible for callingActionmethodselectorThe object implements the above logic and returnsReflectedactiondescriptor. The actionmethodselector object uses reflection to execute the above process. The execution result either returns a methodinfo, returns NULL, or throws an exception. Reflectedcontrollerdescriptor determines the return value. If it is null, null is also returned. If it is methodinfo, It is packaged into reflectedactiondescriptor and returned. For the invokeaction method outside, if reflectedcontrollerdescriptor returns a blank reflectedactiondescriptor, false is returned. Otherwise, the operation continues. The Controller is responsible for calling handleunknownaction when false is returned. This method produces a 404 error by default and can be rewritten.

Looking back at the logic, it is not difficult to get the fact that:Actionnameselectorattribute takes precedence over actionmethodselectorattribute and actionmethodselectorattribute takes precedence over methods without actionmethodselectorattribute..

For more details about the source code, refer to: Studying controller and view (6) in ASP. net mvc through source code)

 

Built-in MVC Filter

WhenActiondescriptor(Reflectedactiondescriptor) after the return, contolleractioninvoker starts to execute the action, but there are some additional steps before executing the action, that isFilter. The following four built-in MVC filter interfaces are available:

    • Iactionfilter
    • Iauthorizationfilter
    • Iexceptionfilter
    • Iresultfilter

The four filters will be comprehensively considered by the controlleractioninvoker framework.PseudoCodeDescribes the logic order in which controlleractioninvoker processes them:

 try {// execute iauthorizationfilterrun each iauthorizationfilter's onauthorization () in sequence () method // All iauthorizationfilters return NULL instead of actionresultif (none of the iauthorizationfilters canceled execution) {run each iactionfilter's onactionexecuting () methodrun the action method // note that run each iactionfilter's onactionexecuted () method (in reverse order) run each iresultfilter's onresultexecuting () methodrun the action result // note that run each iresultfilter's onresultexecuted () method (in reverse order )} // iauthorizationfilter returns actionresultelse {run any action result set by the authorization filters} catch (exception not handled by any action or result filter) {// note that the exception handled by iexceptionfilter is only run each iexceptionfilter's onexception () methodrun any action result set by the exception filters in the try catch block.} 

Notes that need attention are provided in the Code. Some details are discussed in the following section.

In MVCFeatures related to these four interfacesAs follows:

We are familiarAuthorize,Outputcache,Childaction. Once, I couldn't fully understand and classify these various attributes. Now I finally find their respective destinations.

In the next section, we will discuss some details about these four filter interfaces.

 

Labor fruit, reproduced please indicate the source: http://www.cnblogs.com/P_Chou/archive/2010/12/01/details-asp-net-mvc-07.html

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.