ASP. net mvc: Model Binding Mechanism Analysis

Source: Internet
Author: User

Environment:

Windows 2008, vs 2008 SP1, ASP. net mvc RC1

------------------------------------------------------------------------------

This article analyzes the request process segment of action invoking and explores how the model binding in ASP. net mvc is implemented.

Request Process segment:

Before the requested action is called, The controlleractioninvoker. invokeaction () method is called. In this method, an instance of reflectedactiondescriptor is constructed, which contains the description of the action.

Then, controlleractioninvoker. the getparametervalues () method is called to obtain the action parameter list (the corresponding parameterdescriptor instances are created separately) by passing in the previously created reflectedactiondescriptor instance, and then traverse the parameters, try to get the parameter value. In the traversal loop, controlleractioninvoker. the getparametervalue () method is called.

The following is the method of controlleractioninvoker. getparametervalue ()Source code:

Code
Protected   Virtual   Object Getparametervalue (controllercontext, parameterdescriptor ){
// Collect all of the necessary binding properties
Type parametertype = Parameterdescriptor. parametertype;
Imodelbinder Binder
= Getmodelbinder (parameterdescriptor );
Idictionary
< String , Valueproviderresult > Valueprovider = Controllercontext. Controller. valueprovider;
String Parametername = Parameterdescriptor. bindinginfo. prefix ?? Parameterdescriptor. parametername;
Predicate
< String > Propertyfilter = Getpropertyfilter (parameterdescriptor );

// Finally, call into the binder
Modelbindingcontext bindingcontext =   New Modelbindingcontext (){
Fallbacktoemptyprefix
= (Parameterdescriptor. bindinginfo. prefix =   Null ), // Only fall back If prefix not specified
Modelname = Parametername,
Modelstate
= Controllercontext. Controller. viewdata. modelstate,
Modeltype
= Parametertype,
Propertyfilter
= Propertyfilter,
Valueprovider
= Valueprovider
};
Object Result = Binder. bindmodel (controllercontext, bindingcontext );
Return Result;
}

This method obtains an imodelbinder associated with this parameter through the parameter description of parameterdescriptor, and then calls the imodelbinder. bindmodel () method to obtain the parameter value.

Therefore, there are two processes:

1) Confirm imodelbinder

Controlleractioninvoker. getmodelbinder () is called,

Code
Private Imodelbinder getmodelbinder (parameterdescriptor ){
// Look on the parameter itself, then look in the global table
Return Parameterdescriptor. bindinginfo. Binder ?? Binders. getbinder (parameterdescriptor. parametertype );
}

This method first tries to obtain the imodelbinder (read the custommodelbinderattribute of the parameter) from parameterdescriptor. If we do not specify the attribute for the parameter, It is null and it is obtained from the binders, see the definition of this binders:

Code
Protected   Internal Modelbinderdictionary binders {
Get {
If (_ Binders =   Null ){
_ Binders
= Modelbinders. binders;
}
Return _ Binders;
}
Set {
_ Binders
= Value;
}
}

Here, binders references modelbinders. Binders (it is noted that modelbinders is a static class). At this time, modelbinderdictionary is about to take effect. Let's look at the getbind () method in the modelbinderdictionary class:

Code
Public Imodelbinder getbinder (type modeltype ){
Return Getbinder (modeltype, True   /* Fallbacktodefault */ );
}

Public   Virtual Imodelbinder getbinder (type modeltype, Bool Fallbacktodefault ){
If (Modeltype =   Null ){
Throw   New Argumentnullexception ( " Modeltype " );
}

ReturnGetbinder (modeltype, (fallbacktodefault)?Defaultbinder:Null);
}

Private Imodelbinder getbinder (type modeltype, imodelbinder fallbackbinder ){
// Try to look up a binder for this type. We use this order of precedence:
// 1. Binder registered in the global table
// 2. Binder attribute defined on the Type
// 3. supplied fallback Binder

Imodelbinder binder;
If (_ Innerdictionary. trygetvalue (modeltype, Out Binder )){
Return Binder;
}

Binder=Modelbinders. getbinderfromattributes (modeltype,
()
=>String. Format (cultureinfo. currentuiculture, mvcresources. modelbinderdictionary_multipleattributes, modeltype. fullname ));

ReturnBinder??Fallbackbinder;
}

This is the three reloads of the method, which are called in order. When the third overload method is called, it first tries to get the imodelbinder corresponding to modeltype from the _ innerdictionary variable of modelbinderdictionary (previously mentioned that modelbinders is a static class, therefore, we have the opportunity to add the ing of the type and custom imodelbinder to this dictionary. Otherwise, we will try to get it from attribute again. If none of them can be found, then defaultbinder will be returned, here defaultbinder is an instance of defamodelmodelbinder. Finally, the imodelbinder corresponding to the parameter is declared and determined. Next, the parameter is assigned a value through the imodelbinder. bindmode () method.

2) value assignment

The next thing is that the corresponding imodelbinder plays a role, combines the value with controllercontext. Controller. valueprovider, and assigns a value to the parameter.

....

Then the requested action is officially executed.

Summary:

This article only analyzes a very small clip.ArticleContinue to explore.

The following lists the related classes. If you are interested, refer:

Custommodelbinderattribute, modelbinderattribute, bindattritionary, imodelbinder, defaultmodelbinder, modelbinderdictionary, modelbinders, modelbindingcontext, and xxxdescriptor.

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.