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 ...
随机推荐
- bzoj4873 [Shoi2017]寿司餐厅
Input 第一行包含两个正整数n,m,分别表示这家餐厅提供的寿司总数和计算寿司价格中使用的常数. 第二行包含n个正整数,其中第k个数ak表示第k份寿司的代号. 接下来n行,第i行包含n-i+1个整数 ...
- Google Cast和ChromeCast
Google Cast类似于DLNA,AirPlayer,Miracast,就是一种投屏技术.我们ATV产品是对Google Cast和ChromeCast都是支持的. Google Cast 大致工 ...
- CSUST 四月选拔赛个人题解
这场比赛演的逼真,感谢队友不杀之恩 总结:卡题了赶紧换,手上捏着的题尽快上机解决 http://csustacm.com:4803/ 1113~1122 1113:六学家 题意:找出满足ai+aj=a ...
- redis 配置和使用(C++)
一.Redis简介: Redis为非关系型数据库,Redis是一个Key-Value存储系统.它支持存储的value类型有:string(字符串),list(链表), set(无序集合),zset(s ...
- STL源码分析-内存分配器
http://note.youdao.com/noteshare?id=744696e5f6daf0f2f03f10e381485e67
- bzoj 3209 bzoj1799 数位dp
3209: 花神的数论题 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2267 Solved: 1040[Submit][Status][Disc ...
- grafana模板
1.安静了这么久,换了一家公司,还有过年,去了上海,去了苏州,去了杭州,认识了一个人,跟老司机他们一起学k8s,所以很累很累,这是监控,也是在老司机的帮助下熟悉使用,3q!
- Tensorboard教程:显示计算图中节点信息
Tensorboard显示计算图节点信息 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 强烈推荐Tensorflow实战Google深度学习框架 实验平台: Tensorflow1 ...
- 维护前面的position+主席树 Codeforces Round #406 (Div. 2) E
http://codeforces.com/contest/787/problem/E 题目大意:给你n块,每个块都有一个颜色,定义一个k,表示在区间[l,r]中最多有k中不同的颜色.另k=1,2,3 ...
- Atcoder #017 agc017 B.Moderate Differences 思维
LINK 题意:给出最左和最右两个数,要求往中间填n-2个数,使得相邻数间差的绝对值$∈[L,R]$ 思路:其实也是个水题,比赛中大脑宕机似的居然想要模拟构造一个数列,其实我们只要考虑作为结果的数,其 ...