Parameterbindingattribute
In the previous article, we focused on the usage scenarios of modelbinderattribute. This article describes in detail the parameter binding principle behind Modelbinder.
Modelbinderattribute inherits from Parameterbindingattribute, and from the name you can see that Parameterbindingattribute is an attribute that binds an action parameter. In addition to Modelbinderattribute, WEBAPI also defined Valueproviderattribute, Fromuriattribute and Frombodyattribute three parameterbindingattribute derived classes.
[AttributeUsage (AttributeTargets.Class | Attributetargets.parameter, inherited = True, AllowMultiple = False)] public abstract class Parameterbindingattribute:attribute { protected parameterbindingattribute (); Public abstract httpparameterbinding getbinding (httpparameterdescriptor parameter);}
Parameterbindingattribute only provides a getbinding method that returns an Httpparameterbinding type Object, Httpparameterbinding is a core class in the process of parameter binding, which basically completes the whole process of reading from the request data to the parameter binding.
Here you need to focus on httpparameterdescriptor, which is a descriptive object of the parameter, contains a message about the parameter (which will be explained in detail later)
Httpparameterbinding
Public abstract class Httpparameterbinding { protected httpparameterbinding (httpparameterdescriptor descriptor); Public Httpparameterdescriptor descriptor {get;} Public virtual string ErrorMessage {get;} public bool IsValid {get;} Public virtual bool Willreadbody {get;} Public abstract Task Executebindingasync (Modelmetadataprovider metadataprovider, Httpactioncontext Actioncontext, CancellationToken cancellationtoken); Protected Object GetValue (Httpactioncontext actioncontext); protected void SetValue (Httpactioncontext actioncontext, Object value);}
Where the Executebindingasync method implements the specific parameter binding. Data from the URI and body can be distinguished by willreadbody, and Willreadbody defaults to False.
Modelbinderparameterbinding
Now let's go back and look at one of the model bindings.
[AttributeUsage (AttributeTargets.Class | Attributetargets.parameter, inherited = True, AllowMultiple = False)] public class Modelbinderattribute: Parameterbindingattribute {public modelbinderattribute (); Public Modelbinderattribute (Type bindertype); Public Type Bindertype {get; set;} public string Name {get; set;} public bool Suppressprefixcheck {get; set;} public override httpparameterbinding getbinding (httpparameterdescriptor parameter); Public Imodelbinder Getmodelbinder (httpconfiguration configuration, Type modeltype); Public Modelbinderprovider Getmodelbinderprovider (httpconfiguration configuration); Public virtual ienumerable<system.web.http.valueproviders.valueproviderfactory> getvalueproviderfactories ( Httpconfiguration configuration); }
A Getmodelbinderprovider method is defined in modelbinderparameterbinding. A Getbinder method is defined in Modelbinderprovider to get modelbinder.
And Modelbinder is the basic class that completes the model binding.
Modelbinderattribute
Fromuriattribute
Valueproviderattribute
Formatterparameterbinding
For data in the body, WEBAPI provides formatterparameterbinding for data binding. Because the body can provide the data format is not as single as the URI, so our body can be more convenient to provide us with complex data structure. Formatterparameterbinding can be seen by naming it to provide the ability to deserialize the body data and bind it to parameters.
Mediatypeformatter
For different request data formats, Formatterparameter provides different serialization objects based on the conent-type of the request. These serialization processing types are inherited from Mediatypeformatter.
Let me list the correspondence between Content-type and Mediatypeformatter.
Content-type |
Mediatypeformatter |
Text/xml,application/xml |
Xmlmediatypeformatter |
Text/json,application/json |
Mediatypeformatter |
application/x-www-form-urlencoded |
Formurlencodedmediatypeformatter |
application/x-www-form-urlencoded |
Jquerymvcformurlencodedformatter |
Frombodyattribute
Iactionvaluebinder
Public interface Iactionvaluebinder {httpactionbinding getbinding (httpactiondescriptor actiondescriptor); }
Bind the entry.
Httpactionbinding
public class Httpactionbinding {public httpactionbinding (); Public httpactionbinding (Httpactiondescriptor Actiondescriptor, httpparameterbinding[] bindings); Public Httpactiondescriptor Actiondescriptor {get; set;} Public virtual Task Executebindingasync (httpactioncontext actioncontext, CancellationToken cancellationtoken); } }
Parameter separation.
ASP. NET WebAPI 05 parameter binding