由于客户端调用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. Mac/win eclipse genymotion 插件下载地址

    eclipse -->new install --> 填写该地址 (目前最新的地址) https://dl.genymotion.com/eclipse/

  2. gridview根据条件来改变行的颜色以及改变单元格的颜色。

    gridview根据条件来改变行的颜色以及改变单元格的颜色. 通过在RowDataBound事件中写代码来实现,见代码. protected void GridView1_RowDataBound(o ...

  3. asp.net存储过程分页+GridView控件 几百万数据 超快

    存储过程:---亲测275万数据,分页速度N快 ))+' '+@orderid+' from '+@tablename+' '+@tmpOrderid set @sql='select top'+st ...

  4. 一些liunx base-fs、mini-fs、docker image 系统 安装kernel、grub文件,使之独立运行的注意事项

    如题 通常你不会顺利的启动成功的! 其原因在于 init 初始化管理系统 ,主要是systemd在作祟! 要么官方没有安装,要么安装的是定制多的删减版,故意是base系统无法启动! 怎么办? 彻底删除 ...

  5. NAS 创建大文件

      不是很懂,但是管用.先记录下来. http://www.111cn.net/sys/linux/55537.htm

  6. PHP导出excel文件的几种方式

    PHP导出excel文件的几种方式 先说说动态生成的内容当作文件来下载的方法: 1.通过把Content-Type设置为application/octet-stream,可以把动态生成的内容当作文件来 ...

  7. Xcode文件被锁定:The file ".xcodeproj" could not be unlocked

    同事从svn上面checkout项目到本地,通过xcode打开的时候提示的这个问题. The file "xcodeproj" could not be unlocked. Cou ...

  8. Spring boot配置log4j输出日志

    1. pom.xml文件中配置parent,版本选定[1.2.5.RELEASE] 关于为什么要选这个版本:我尝试使用[1.4.1.RELEASE],但该版本库里没有[spring-boot-star ...

  9. 转载linux性能调优工具

    Linux 大牛,Netflix 高级性能架构师 Brendan Gregg 更新 Linux 性能调优工具,各种资源应有尽有,大量干货,强烈建议收藏.

  10. osgExp只能将3dmax中的动画导出为路径动画osg::AnimationPath,而不能导出osgAnimation::Animation。osg播放骨骼动画应该使用FBX格式

    通过实际的模型测试,导出为.osg文本格式,搜索animation,只能搜索到AnimationPathCallback,而搜索不到osgAnimation相关类 在OSGExp1.5.0源代码中搜索 ...