由于客户端调用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. IntelliJ IDEA 的 .idea 目录加入.gitignore无效的解决方法

    [转载] 无效的原因是:对应的目录或者文件已经被git跟踪,此时再加入.gitignore后就无效了, 解决办法: 先执行 [文件夹]  git rm -r --cached .idea [文件]   ...

  2. UITextView: 响应键盘的 return 事件

    UITextFieldDelegate代理里面响应return键的回调:textFieldShouldReturn:.但是 UITextView的代理UITextViewDelegate 里面并没有这 ...

  3. 如何解析oracle执行计划

    要执行任何SQL语句,Oracle 必须推导出一个“执行计划”.查询的执行计划是 Oracle 将如何实现数据的检索,以满足给定 SQL 语句的描述.它只不过是其中包含的步骤及它们之间关系的顺序树.执 ...

  4. fedora26在编译s3c2440内核时make menuconfig *** Unable to find the ncurses libraries

    [root@fedora-26 linux-2.6.32.2]# make menuconfig *** Unable to find the ncurses libraries or the *** ...

  5. [SQLite3]connection string的连接池参数引发的错误

    最近在.net中使用Sqlite数据库,发现.net的驱动做得不错,而且实现了加密功能.于是想给自己的数据库加上口令,结果,多次实验都以失败告终: 链接数据库,然后ChangePassword都成功执 ...

  6. python的redis简单使用

    安装的Python版本 3.6.1 redis安装的2.8 安装redis模块 pip install redis 简单使用 redis-test.py import redis r=redis.Re ...

  7. SpEL、PropertyPlaceholderConfigurer与@Value、#{}、${}

    概念 SpEL:Spring EL表达式 PropertyPlaceholderConfigurer:即org.springframework.beans.factory.config.Propert ...

  8. Android Studio 视图解析

    AS一共同拥有三种视图.我们来分别分析每一种视图的作用. 一.Project视图.(白色字体的文件夹/文件可不关注) 图片中的链接 Gralde介绍:http://stormzhang.com/dev ...

  9. Unity使用OpenGL绘制线段

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class ShowGrid ...

  10. 安装ubuntu后,你的屏幕尺寸太小,无法设置,该怎么解决

    安装完虚拟机之后,你的Ubuntu可能会在尺寸很小,(这种情况可能有,可能没有) 想要点击设置,选中Display里的分辨率下拉框,但是却因为这个窗口太大,无法点击apply按钮.悲剧啦!!! Ctr ...