asp.net mvc three important descriptive objects: Controllerdescriptor

Source: Internet
Author: User

asp.net MVC application requests are for a certain action method of a controller, so the processing of the request ultimately manifests itself in the execution of the target action method. The action method has the appropriate parameters, so you must extract the corresponding data from the request and convert it to the action method argument list before the method executes, which we call the model binding. In the ASP.net MVC application programming interface, the metadata for a parameter of the action method is represented by Parameterdescriptor, while the two The associated types Controllerdescriptor and actiondescriptor are used to describe the Controller and action methods. [This article has been synchronized to ' how ASP.net MVC works ']

First, Controllerdescriptor

Controllerdescriptor contains metadata information that describes a controller. As shown in the following code fragment, Controllerdescriptor has three properties, where Controllername and Controllertype represent controller names and types, which originate from routing information; characters The UniqueID of the string type represents the unique identity of the Controllerdescriptor, which is derived from its own type, the type of controller, and the name of the controller.

1:public Abstract class Controllerdescriptor:icustomattributeprovider
2: {
3:public Virtual object[] 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 Actiondescriptor findaction (ControllerContext controllercontext, string actionname);
9:public abstract actiondescriptor[] getcanonicalactions ();
10:
11:public Virtual string Controllername {get;}
12:public abstract Type Controllertype {get;}
13:public Virtual string UniqueId {get;}
14:}
15:
16:public Interface ICustomAttributeProvider
17: {
18:object[] GetCustomAttributes (bool inherit);
19:object[] GetCustomAttributes (Type attributetype, bool inherit);
20:bool isdefined (Type attributetype, bool inherit);
21:}

The Controllerdescriptor implements the ICustomAttributeProvider interface, which means that we can get the application in the GetCustomAttributes and GetCustomAttributes methods by invoking the You can also call the IsDefined method to determine whether the specified custom attribute type is applied to the corresponding controller type controller all custom attributes on the type or attributes of a given type.

Another method getfilterattributes is used to get all the filter attributes applied to the controller (inherited from the abstract class FilterAttribute). A filter is an AOP based design that allows us to inject the execution of some of the relevant logic based on crosscutting concerns into the execution of the action method, and we will describe the filter in detail in the execution of the action method.

The Controllerdescriptor Findaction method obtains the corresponding action method based on the specified controller context and name, and returns the Actiondescriptor object used to describe the action method. The getcanonicalactions gets all the action methods of the current controller, and the return type is a actiondescriptor array.

Second, Reflectedcontrollerdescriptor

The

Defines the unique inherited type Reflectedcontrollerdescriptor of an abstract class controllerdescriptor in the ASP.net MVC application programming interface. As the name suggests, reflectedcontrollerdescriptor resolves the metadata used to describe controller through the mechanism of reflection. The Controllertype property representing the controller type is specified in the constructor, as shown in the following code fragment. Reflectedcontrollerdescriptor gets the relevant attributes applied on the controller type by reflection to provide implementations for the ICustomAttributeProvider interface.

   1:public class Reflectedcontrollerdescriptor:controllerdescriptor
2: {
3: Public reflectedcontrollerdescriptor (Type controllertype);
4:
5: Public override object[] GetCustomAttributes (bool inherit);
6: Public override object[] GetCustomAttributes (Type attributetype, bool inherit);
7: Public override ienumerable<filterattribute> Getfilterattributes (bool usecache);
8: Public override bool IsDefined (Type attributetype, bool inherit);
9:
(a) public override Actiondescriptor Findaction (ControllerContext controllercontext, string actionname);
One: Public override actiondescriptor[] Getcanonicalactions ();
12:
sealed override Type Controllertype {get;}
14:}

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.