由于客户端调用Web API传递的数据属性命名一般偏向javascript规范,只是简单的大小写差异没有问题,但始终会有一些特殊情况。比如OAuth的请求:

client_id : "value"
client_secret : "value"

在ASP.NET MVC开发时一般我们会开发一个ModelBinder,如果只是实现别名的绑定,继承DefaultModelBinder即可快速实现。下面的BindAliasModelBinder就是一个简单的实现参考:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class BindAliasAttribute : Attribute
{
public BindAliasAttribute(string name)
{
this.Name = name;
} public string Name { get; set; }
}
public abstract class AttributeModelBinder<TAttribute> : DefaultModelBinder
where TAttribute : Attribute
{
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
base.BindProperty(controllerContext, bindingContext, propertyDescriptor); foreach (var attribute in propertyDescriptor.Attributes)
{
if (attribute is TAttribute)
{
BindPropertyCore(controllerContext, bindingContext, propertyDescriptor, attribute as TAttribute); break;
}
}
} protected abstract void BindPropertyCore(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, TAttribute attribute);
}
public class BindAliasModelBinder : AttributeModelBinder<BindAliasAttribute>
{
protected override void BindPropertyCore(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, BindAliasAttribute attribute)
{
var value = controllerContext.HttpContext.Request.Params[attribute.Name];
propertyDescriptor.SetValue(bindingContext.Model, value);
}
}

然后我们在模型上这么使用它:

[ModelBinder(typeof(BindAliasModelBinder))]
public class OAuthModel
{
[BindAlias("client_id")]
public string ClientId { get; set; } [BindAlias("redirect_uri")]
public string RedirectUri { get; set; }
}

再来看Web API的模型绑定:System.Web.Http.ModelBinding.IModelBinder。只定义了一个方法"BindModel",需要实现和上面BindAliasModelBinder一样的功能有点太复杂了。这里有一个比较偷懒的推荐方式,就是将参数定义为一个JObject对象,调用它提供的ToObject<T>方法转换为实际的模型,这个时候就可以通过JsonPropertyAttribute解决映射的问题。例如:

public AccessTokenResponse AccessToken(JObject content)
{
var request = content.ToObject<AccessTokenRequest>(); }

关于ASP.NET Web API的ModelBinding杂谈的更多相关文章

  1. ASP.NET Web API Model-ActionBinding

    ASP.NET Web API Model-ActionBinding 前言 前面的几个篇幅把Model部分的知识点划分成一个个的模块来讲解,而在控制器执行过程中分为好多个过程,对于控制器执行过程(一 ...

  2. ASP.NET Web API Model-ParameterBinding

    ASP.NET Web API Model-ParameterBinding 前言 通过上个篇幅的学习了解Model绑定的基础知识,然而在ASP.NET Web API中Model绑定功能模块并不是被 ...

  3. ASP.NET Web API Model-ModelBinder

    ASP.NET Web API Model-ModelBinder 前言 本篇中会为大家介绍在ASP.NET Web API中ModelBinder的绑定原理以及涉及到的一些对象模型,还有简单的Mod ...

  4. 在ASP.NET Web API中使用OData

    http://www.alixixi.com/program/a/2015063094986.shtml 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在A ...

  5. 使用ASP.NET Web API 2创建OData v4 终结点

    开放数据协议(Open Data Protocol[简称OData])是用于Web的数据访问协议.OData提供了一种对数据集进行CRUD操作(Create,Read,Update,Delete)的统 ...

  6. 【ASP.NET Web API教程】6.4 模型验证

    本文是Web API系列教程的第6.4小节 6.4 Model Validation 6.4 模型验证 摘自:http://www.asp.net/web-api/overview/formats-a ...

  7. Asp.Net Web API 2第十六课——Parameter Binding in ASP.NET Web API(参数绑定)

    导航 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html. 本文主要来讲解以下内容: ...

  8. Asp.Net Web API 2第十五课——Model Validation(模型验证)

    前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文参考链接文章地址htt ...

  9. ASP.NET Web API中使用OData

    在ASP.NET Web API中使用OData 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在ASP.NET Web API中,对于CRUD(creat ...

随机推荐

  1. .NET Best Practices

    Before starting with best practices tobe followed, it is good to have clear understanding of how mem ...

  2. EA修改生成代码的表头注释

    我们在做项目的过程中,每个代码文件都应有此文件的注释,比如说作者,文件说明等.但是如果用EA生成的代码文件的注释是纯英文的,而且有些不是我们需要显示的注释,有些我们需要显示的它又不具备.那么我们就可以 ...

  3. 各大公司Java面试题超详细总结

    ThreadLocal(线程变量副本)Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量.采用空间换时间,它用于线程间的数据隔离,为每一个使用该变量的线程提供一个副 ...

  4. 新浪股票接口AndroidSDK

    昨天想到一个点子,需要访问股票行情.于是在网上搜了一下免费的股市行情的接口.发现新浪股票的数据接口比较稳定,于是就用它了. 网上对于新浪股票的数据接口介绍比较详细,并且实现也很简单,所以花了一下午就基 ...

  5. Unity用代码实现Remove Missing Script

    [MenuItem("Edit/Cleanup Missing Scripts")] static void CleanupMissingScripts () { ; i < ...

  6. jquery判断某个属性是否存在 hasAttr

    在JQuery编码中,我们会判断元素是否存在某个属性.比如是否包含 class="new" 的样式呢.JQuery判断就非常简单了,因为有 hasClass这个方法 $(" ...

  7. ./configure、make、make install 命令

    https://www.cnblogs.com/tinywan/p/7230039.html https://www.sohu.com/a/191735643_505857 ./configure 该 ...

  8. 如何将数组中的后面m个数移动为前面m个数

    思路分析: 可以通过递归的方法实现调整: (1)将前n-m个元素的顺序颠倒. (2)将后面m个元素的顺序颠倒. (3)将n个元素的顺序全部颠倒. 通过以上3个步骤的执行,就可以把数组的元素颠倒. 代码 ...

  9. Java按钮控件数组实现计算器界面

    编写程序,通过按钮数组来管理界面中的所有按钮控件,从而使用最少的代码实现模拟的计算器界面. 思路如下: 创建一个类,通过extends使其继承窗体类JFrame: 创建一个JFrame对象,使用JFr ...

  10. [AX]AX2012 R2 采购订单的“Request change”

    在采购订单List或者Detail窗口的操作面板上有一个叫做“Request change”的按钮,如果这个按钮是激活的,采购订单不能直接编辑,而必须先使用这个按钮请求修改后,采购订单才能进入编辑状态 ...