asp.net mvc three important descriptive objects: Actiondescriptor

Source: Internet
Author: User
Tags abstract bool types of filters

The Controllerdescriptor object used to describe it is created by the activated controller type during the model binding. Controller is a collection of action methods, and each action is represented by the Actiondescriptor object, and in this article we focus on the different types of actiondescriptor. [This article has been synchronized to ' how ASP.net MVC works ']

First, Actiondescriptor

The actiondescriptor definition used to describe the action method defined in the Controller class is as follows. The properties ActionName and Controllerdescriptor represent the name of the action and the Controllerdescriptor object that describes the controller. The UniqueID property that represents the unique identity derives from its own type, the type of the controller, and the action name.

 1:public abstract class Actiondescriptor:icustomattributeprovider 
2: {
3:public virtual objec T[] GetCustomAttributes (bool inherit);
4:public Virtual object[] GetCustomAttributes (Type attributetype, bool inherit);
5:public virtual bool isdefined (Type attributetype, bool inherit);
6:public Virtual ienumerable<filterattribute> getfilterattributes (bool usecache);
7:
8:public abstract parameterdescriptor[] GetParameters ();
9:public Abstract Object Execute (ControllerContext controllercontext, idictionary<string, object> parameters);
10:public Virtual icollection<actionselector> getselectors ();
11:public Virtual FilterInfo getfilters ();
12: 
13:public abstract string ActionName {get;}
14:public abstract controllerdescriptor controllerdescriptor {get;}
15:public Virtual string UniqueId {get;}
:}

Like Controllerdescriptor, Actiondescriptor also implements the method defined in the ICustomAttributeProvider interface, and we can get the relevant properties applied to the action method by the corresponding method, Or determine whether a specified attribute is applied to the corresponding action method. The Getfilterattributes method returns all the filter attributes applied to the action method. The Parameterdescriptor array that describes all the parameters in the action method is returned through the method GetParameters. The execution of the action method can be done directly by calling method execute, and the method's two parameters ControllerContext and parameters represent the controller context and the incoming parameter, respectively, of the action method execution.

The Getselectors method is used to return a set of objects of type Actionselector that represent the action selector, and Actionselector is a delegate type. As the following code fragment shows, the Actionselector delegate has a unique parameter of type ControllerContext, and the return value of the Boolean type indicates whether the target action method matches the specified controller context. By default, this method returns an empty Actionselector collection.

   1:public delegate bool Actionselector (ControllerContext controllercontext);

The Actiondescriptor Getfilters method returns an object of FilterInfo type, through which we can get all the filters applied to the action method. As shown in the following code, FILTERINFO has four read-only collection properties, which are applied to four types of filters on the action method (Actionfilter, Authorizationfilter, Exceptionfilter, and Resultfilter).

   1:public class FilterInfo
2: {
3: Public ilist<iactionfilter> actionfilters {get;}
4: Public ilist<iauthorizationfilter> authorizationfilters {get;}
5: Public ilist<iexceptionfilter> exceptionfilters {get;}
6: Public ilist<iresultfilter> resultfilters {get;}
7:}

Second, Asyncactiondescriptor

The asynchronous version of Actiondescriptor is represented by the Asyncactiondescriptor type, which is used to describe the asynchronous method defined in Asynccontroller. As shown in the following code snippet, Asyncactiondescriptor is an abstract class that inherits from Actiondescriptor, overrides the Execute method, and defines two abstract methods for executing the action method asynchronously Beginexecute/endexecute.

   1:public Abstract class Asyncactiondescriptor:actiondescriptor
2: {
3: Public abstract IAsyncResult beginexecute (ControllerContext controllercontext, Idictionary<string, Object > Parameters, AsyncCallback callback, object state);
4: Public Abstract Object EndExecute (IAsyncResult asyncresult);
5: Public Override Object Execute (ControllerContext controllercontext, idictionary<string, object> parameters);
6:}

In fact, the Asyncactiondescriptor rewrite of the Execute method does not implement the logic of any action method execution, but rather throws a InvalidOperationException exception directly, meaning used to synchronize The Execute method that performs the action is not valid here.

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.