modelbinding about ASP. NET Web API

Source: Internet
Author: User

Because the client calls the Web API to pass the data property naming generally biased toward the JavaScript specification, there is no problem with simple case differences, but there are always some special cases. For example, OAuth requests:

CLIENT_ID: "Value""value"

When ASP. NET MVC development, we usually develop a modelbinder, if we just implement the binding of aliases, inherit Defaultmodelbinder can be implemented quickly. The following bindaliasmodelbinder is a simple implementation reference:

false true )]publicclass  bindaliasattribute:attribute{    public Bindaliasattribute (string  name)    {        this. name = name;    }      Public string Get Set ; }}
 Public Abstract classAttributemodelbinder<tattribute>: Defaultmodelbinderwheretattribute:attribute{protected Override voidBindProperty (ControllerContext controllercontext, Modelbindingcontext BindingContext, PropertyDescriptor PropertyDescriptor) {Base.        BindProperty (ControllerContext, BindingContext, PropertyDescriptor); foreach(varAttributeinchpropertydescriptor.attributes) {if(Attribute isTattribute) {Bindpropertycore (ControllerContext, BindingContext, PropertyDescriptor, attribute asTattribute);  Break; }        }    }    protected Abstract voidBindpropertycore (ControllerContext controllercontext, Modelbindingcontext BindingContext, PropertyDescriptor PropertyDescriptor, Tattribute attribute);}
 Public class Bindaliasmodelbinder:attributemodelbinder<bindaliasattribute>{    protected  Overridevoid  bindpropertycore (controllercontext controllercontext, Modelbindingcontext BindingContext, PropertyDescriptor propertydescriptor, Bindaliasattribute attribute)    {        var value = Controllercontext.httpcontext.request.params[attribute. Name];        Propertydescriptor.setvalue (Bindingcontext.model, value);}    }

Then we use it on the model:

[Modelbinder (typeof(Bindaliasmodelbinder))] Public classoauthmodel{[Bindalias ("client_id")]     Public stringClientId {Get;Set; } [Bindalias ("Redirect_uri")]     Public stringRedirecturi {Get;Set; }}

Then look at the model bindings for the Web API: System.Web.Http.ModelBinding.IModelBinder. Only one method "Bindmodel" is defined, and it is a bit too complex to implement the same functionality as above Bindaliasmodelbinder. Here is a lazy recommendation, is to define the parameter as a Jobject object, call it provides the Toobject<t> method to convert to the actual model, this time can be solved by jsonpropertyattribute mapping problem. For example:

 Public accesstokenresponse Accesstoken (jobject content) {    var request = content. Toobject<accesstokenrequest>();}

modelbinding about ASP. NET Web API

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.