ASP.NET WebAPI 05 参数绑定
ParameterBindingAttribute
在上一篇中重点讲了ModelBinderAttribute的使用场景。这一篇详细的讲一下ModelBinder背后的参数绑定原理。
ModelBinderAttribute继承自ParameterBindingAttribute,从命名上就是可以看出ParameterBindingAttribute是对Action参数进行绑定的一种特性。除了ModelBinderAttribute之外,WebAPI还另外定义了ValueProviderAttribute,FromUriAttribute与FromBodyAttribute三个ParameterBindingAttribute派生类。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Parameter, Inherited = true, AllowMultiple = false)]
public abstract class ParameterBindingAttribute : Attribute
{
protected ParameterBindingAttribute();
public abstract HttpParameterBinding GetBinding(HttpParameterDescriptor parameter);
}
ParameterBindingAttribute只提供了一个GetBinding方法,这个方法返回一个HttpParameterBinding类型对象,HttpParameterBinding是参数绑定过程中的一个核心类,它基本完成了从请求数据读取到参数绑定的整个过程。
在这里需要重点关注一下HttpParameterDescriptor,它做作为对参数的一个描述对象,包含了参数的一个信息(这个参后续还将详细讲解)
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);
}
其中ExecuteBindingAsync方法实现具体的参数绑定。对于来自Uri与Body的数据可通过WillReadBody进行区分,WillReadBody默认为false。
ModelBinderParameterBinding
现在我们回过头去再看上一篇的Model绑定。
[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);
}
在ModelBinderParameterBinding中定义了一个GetModelBinderProvider方法。ModelBinderProvider中定义一个GetBinder方法用于获取ModelBinder。
而ModelBinder正是完成Model绑定的基础类。
ModelBinderAttribute
FromUriAttribute
ValueProviderAttribute
FormatterParameterBinding
对于Body中的数据,WebAPI提供了FormatterParameterBinding进行数据绑定。由于Body可以提供的数据格式不像Uri那样单一,所以我们Body能够更加方便的为我们提供复杂的数据结构。FormatterParameterBinding从命名就可以看出来它提供将Body数据反序列化并绑定到参数的功能。
MediaTypeFormatter
对于不同的请求数据格式,FormatterParameter会根据请求的Conent-Type,提供不同的序列化对象。而这些序列化处理类型都继承于MediaTypeFormatter。
下面我列举一下Content-Type与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);
}
绑定入口。
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);
}
}
参数分离。
ASP.NET WebAPI 05 参数绑定的更多相关文章
- Parameter Binding in ASP.NET Web API(参数绑定)
Parameter Binding in ASP.NET Web API(参数绑定) 导航 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnbl ...
- 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. 本文主要来讲解以下内容: ...
- WebAPI 2参数绑定方法
简单类型参数 Example 1: Sending a simple parameter in the Url [RoutePrefix("api/values")] public ...
- ASP.NET WebAPI 11 参数验证
在绑定完Action的所有参数后,WebAPI并不会马上执行该方法,而要对参数进行验证,以保证输入的合法性. ModelState 在ApiController中一个ModelState属性用来获取参 ...
- ASP.NET WebAPI 04 Model绑定
在前面的几篇文章中我们都是采用在URI中元数据类型进行传参,实际上ASP.NET Web API也提供了对URI进行复杂参数的绑定方式--Model绑定.这里的Model可以简单的理解为目标Ancti ...
- Asp.net WebAPI Request参数验证-请不要重复造轮子
随着web客户端的发展,现在很多公司都有专业的前端开发,做到系统前后端分离.ap.net后端典型的就是采用webapi,但是发现很多时候大家对webapi并不了解,这里我们来说说输入参数的验证.前一段 ...
- WebApi 参数绑定方法
WebAPI 2参数绑定方法 简单类型参数 Example 1: Sending a simple parameter in the Url 01 02 03 04 05 06 07 08 09 ...
- [译]WebAPI下的如何实现参数绑定
本文将概述在WebAPI方式下将如何将参数绑定到一个action方法,包括参数是如何被读取,一系列规则决定特定环境采用的那种绑定方式,文章最后将给出一些实际的例子. Parameter binding ...
- 使用ASP.Net WebAPI构建REST服务(四)——参数绑定
默认绑定方式 WebAPI把参数分成了简单类型和复杂类型: 简单类型主要包括CLR的primitive types,(int.double.bool等),系统内置的几个strcut类型(TimeSpa ...
随机推荐
- ndk如何将代码放在jni之外
LOCAL_PATH := $(call my-dir)SDK_PATH := ../../.. include $(CLEAR_VARS)LOCAL_MODULE := libiconv_stati ...
- SPOJ DQUERY 离线树状数组+离散化
LINK 题意:给出$(n <= 30000)$个数,$q <= 2e5$个查询,每个查询要求给出$[l,r]$内不同元素的个数 思路:这题可用主席树查询历史版本的方法做,感觉这个比较容易 ...
- 理解 CSS 中的伪元素 :before 和 :after
CSS 的主要目的是给 HTML 元素添加样式,然而,在一些案例中给文档添加额外的元素是多余的或是不可能的.事实上 CSS 中有一个特性允许我们添加额外元素而不扰乱文档本身,这就是“伪元素”. 你一定 ...
- jQuery 写的简单打字游戏
var off_x; //横坐标 var count=0; //总分 var speed=5000; //速度,默认是5秒. var keyErro=0; //输入错误次数 var keyRight= ...
- asp.net DataTable导出 excel的方法记录(第三方)
官网:http://npoi.codeplex.com/ 简单应用,主要是可以实现我们想要的简单效果,呵呵 需要引入dll,可以在官网下载,也可在下面下载 protected void getExce ...
- 【BZOJ】1087: [SCOI2005]互不侵犯King
[算法]状态压缩型DP [题解]http://www.cnblogs.com/xtx1999/p/4620227.html (orz) https://www.cnblogs.com/zbtrs/p/ ...
- 【Atcoder】AGC 020 B - Ice Rink Game 递推
[题意]n个人进行游戏,每轮只保留最大的a[i]倍数的人,最后一轮过后剩余2人,求最小和最大的n,或-1.n<=10^5. [算法]递推||二分 [题解]令L(i),R(i)表示第i轮过后的最小 ...
- iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2014年2月19日更新版)
//转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3496069.html 编号 iOS-Apple苹果官方文档翻译名称 博文链接地址 ...
- 阿里云 配置FTP 无法连接问题,2017年7月后
被阿里云坑飞,2017年7月上线的安全组,我压根不知道 新服务器 配置防火墙后怎么也不起作用,也暂时关不了防火墙 iptables stop #无效 搞了半天 才知道TM 不需要在linx服务器上自 ...
- 【洛谷 P3199】 [HNOI2009]最小圈(分数规划,Spfa)
题目链接 一开始不理解为什么不能直接用\(Tarjan\)跑出换直接求出最小值,然后想到了"简单环",恍然大悟. 二分答案,把所有边都减去\(mid\),判是否存在负环,存在就\( ...