ASP.NET Web API中的参数绑定总结
ASP.NET Web API中的action参数类型可以分为简单类型和复杂类型。
HttpResponseMessage Put(int id, Product item)
id是int类型,是简单类型,item是Product类型,是复杂类型。
简单类型实参值从哪里读取呢?
--一般从URI中读取
所谓的简单类型包括哪些呢?
--int, bool, double, TimeSpan, DateTime, Guid, decimal, string,以及能从字符串转换而来的类型
复杂类型实参值从哪里读取呢?
--一般从请求的body中读取
复杂类型实参值是否可以从URI中获取呢?
--可以,按如下
→ 有这样的一个类
public class Shape
{
public double Width{get;set;}
public double Length{get;set;}
}
→ 想从URI中获取,那就加上[FromUri]
public HttpResponseMessage Get([FromUri] Shape shape)
→ 客户端就可以放在查询字符串中传
...api/products/?Width=88&Length=199
简单类型可以从请求的body中获取吗?
--可以。按如下:
→ action方法
public HttpResponseMessage Post([FromBody] string name){...}
→ 前端请求中
Content-Type:applicaiton/json
"hello world"
API服务端会根据Content-Type的值选择合适的媒体类型。
复杂类型是否可以从uri中的字符串获取呢?
--可以
api/products/?shape=188,80
如何把uri中查询字符串中shape的字段值,即以逗号分割的字符串转换成Shape类实例呢?
--使用TypeConverter类
[TypeConverter(typeof(ShapeConverter))]
public class Shape
{
public double Width{get;set;}
public double Length{get;set;} public static bool TryParse(string s, out Shampe result)
{
result = null;
var parts = s.Split(',');
if(parts.lenth != )
{
return false;
} double width, length; if(double.TryParse(parts[], out width) && double.TryParse(parts[], out length))
{
result = new Shape(){Width = width; Length = length};
return true;
}
return false;
}
} public class ShapeConverter: TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourcType)
{
if(sourceType == typeof(string))
{
return true;
} return base.CanConvertFrom(context, sourceType);
} public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo, object value)
{
if(value is string)
{
Shape shape;
if(Shape.TryParse((string)value, out shape))
{
return shape;
}
}
return base.ConvertFrom(context, culture, value);
}
}
→ 在action不需要[FromUri]
public HttpResponseMessage Get(Shape shape)
→ 客户端
api/products/?shape=188,80
是否可以通过Model Binder来实现自定义参数绑定过程呢?
--可以,有IModelBinder接口,提供了BindModel方法
→ 自定义一个Model Binder
public class ShapeModelBinder : IModelBinder
{
private static ConcurrentDictionary<string, Shape> _shapes = new ConcurrentDictionary<string, Shape>(StringComparer.OrdinalIgnoreCase); static ShapeModelBinder()
{
_shapes["shape1"] = new Shape(){Width= , Length = };
_shapes["shape2"] = new Shape(){Width=, Length = };
} public bool BindModel(HttpActionContext actionContext, ModelBindingContect biningContext)
{
if(bindingContext.ModelType != typeof(Shape))
{
return false;
} ValueProviderResult val = bindingContext.ValueProvider.GetValue(bidingContext.ModelName);
if(val == null)
{
return false;
} string key = val.RawValue as string;
if(key == null){
bdingContext.ModelState.AddModelError(bindingContext.ModelName, "值类型错误");
return false;
} Shape shape;
if(_shapes.TryGetValue(key, out shape) || Shape.TryParse(key, shape))
{
bindingContext.Model = result;
return true;
} bindingContext.ModelState.AddModelError(bindingContext.ModelName, "无法把字符串转换成Shape");
return false;
}
}
● 从BindingContext中的ValueProvider属性获取到ValueProviderResult
● 从前端查询字符串中传来的字符串,被放在ValueProviderResult的RawValue属性中
● 把字符串转换成Shape实例,最终放在了BindingContext的Model属性中
→ 使用自定义的Model Binder
可以运用在action中:
public HttpResposneMessage Get([ModelBinder(typeof(ShapeModelBinder))] Shape shape);
可以放在模型上:
[ModelBinder(typeof(Shape))]
public class Shape
{ }
也可以放在全局注册中:
public static class WebApiConfig
{
public static void Register(HttpConfiguraiton config)
{
var provider = new SimpleModelBinderProvider(typeof(Shape), new ShapeModelBinder());
config.Services.Insert(typeof(ModelBinderProvider), , provider);
}
}
注意:即使在全局注册,也需要在action中按如下写:
public HttpResponseMessage Get([ModelBinder] Shape shape);
是否可以通过Value Provider来自定义参数绑定过程呢?
--可以。
比如,从前端cookie中获取值,自定义一个Value Provider.
public class MyCookieValueProvider : IValueProvider
{
private Dictionary<string, string> _values; public MyCookieValueProvider(HttpActionContext actionContext)
{
if(actionContext == null)
{
throw new ArgumentNullException("actionContext");
} _values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach(var cookie in actionContext.Request.Headers.GetCookies())
{
foreach(CookieState state in cookie.Cookies)
{
_values[state.Name] = state.Value;
}
}
} public bool COntainsPrefix(string prefix)
{
return _values.keys.Contains(prefix);
} public ValueProviderResult GetValue(string key)
{
string value;
if(_values.TryGetValue(key, out value))
{
return new ValueProviderResult(value, value, CultureInfo.InvariantCulture);
}
return null;
}
}
同时还需要一个ValueProviderFactory.
public class MyCookieValueProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(HttpActionContext actionContext)
{
return new MyCookeValueProvider(actionContext);
}
}
最后注册到全局中。
public static void Register(HttpConfiguration config)
{
config.Services.Add(typeof(ValueProviderFactory), new MyCookieValueProviderFactory());
}
还可以把自定义的ValueProvider放在action中。
public HttpResponseMessage Get([ValueProvider(typeof(MyCookieValueProviderFactory))] Shape shape);
是否可以通过HttpParameterBinding实现参数绑定自定义呢?
--可以。
ModelBinderAttribute继承于ParameterBindingAttribute.
public abstract class ParameterBindingAttribute : Attribute
{
public abstract HttpParameterBinding GetBinding(HttpParameterDescriptor parameter);
}
HttpParameterBinding用来把值绑定到参数上。
假设,需要从前端请求的if-match和if-none-match字段获取ETag值。
public class ETag
{
public string Tag{get;set;}
}
可能从if-match获取,也可能从if-none-match获取,来个枚举。
public enum ETagMatch
{
IfMatch,
IfNoneMatch
}
自定义HttpParameterBinding。
public class ETagParameterBinding : HttpParameterBinding
{
ETagMatch _match; public ETagParameterBinding(HttpParameterDescriptor parameter, ETagMatch match) : base(parameter)
{
_match = match;
} public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken candellationToken)
{
EntityTagHeaderValue etagHeader = null;
switch(_match)
{
case ETagMatch.IfNoneMatch:
etagHeader = actionContext.Request.Headers.IfNoneMatch.FirstOrDefault();
break;
case ETagMatch.IfMatch:
etagHeader = actionContext.Request.Headers.IfMatch.FirstOrDefault();
break;
} ETag etag = null;
if(etagHeader != null)
{
etag = new ETag{Tag = etagHeader.Tag};
} actionContext.ActionArguemnts[Descriptor.ParameterName] = etag; var tsc = new TaskCompletionSource<object>();
tsc.SetResult(null);
return tsc.Task;
}
}
可见,所有的action参数放在了ActionContext的ActionArguments中的。
如何使用自定义的HttpParameterBinding呢?
--通过自定义一个ParameterBindingAttribute特性。
public abstract class ETagMatchAttribute : ParameterBindingAttribute
{
private ETagMatch _match; public ETagMatchAttribute(ETagMatch match)
{
_match = match;
} public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter)
{
if(parameter.ParameterType == typeof(ETag))
{
return new ETagParameterBinding(parameter, _match);
}
return parameter.BindAsError("参数类型不匹配");
}
} public class IfMatchAttribute : ETageMatchAttribute
{
public IfMatchAttribute(): base(ETagMatch.IfMatch)
{}
} public class IfNoneMatchAttribute: ETagMatchAttribute
{
public IfNoneMatchAttribute() : base(ETagMatch.IfNoneMatch)
{}
}
再把定义的有关HttpParameterBinding的特性运用到方法上。
public HttpResponseMessage Get([IfNoneMatch] ETag etag)
还需要在全局注册:
config.ParameterBindingRules.Add(p => {
if(p.ParameterType == typeof(ETag) && p.ActionDescriptor.SupportedHttpMethods.Contains(HttpMethod.Get))
{
return new ETagParameterBinding(p, ETagMatch.IfNoneMatch);
}
else
{
return null;
}
})
总结,本篇体验了简单类型和复杂类型获取前端数据的方式。并通过自定义ValueProvider, ModelBinder, HttpParameterBinding来实现对参数绑定过程的控制。
ASP.NET Web API中的参数绑定总结的更多相关文章
- ASP.NET Web API中的Controller
虽然通过Visual Studio向导在ASP.NET Web API项目中创建的 Controller类型默认派生与抽象类型ApiController,但是ASP.NET Web API框架本身只要 ...
- ASP.NET Web API中的JSON和XML序列化
ASP.NET Web API中的JSON和XML序列化 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok ...
- 利用查询条件对象,在Asp.net Web API中实现对业务数据的分页查询处理
在Asp.net Web API中,对业务数据的分页查询处理是一个非常常见的接口,我们需要在查询条件对象中,定义好相应业务的查询参数,排序信息,请求记录数和每页大小信息等内容,根据这些查询信息,我们在 ...
- 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化
谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...
- Asp.Net Web API 2第十三课——ASP.NET Web API中的JSON和XML序列化
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文描述ASP.NET W ...
- 【ASP.NET Web API教程】4.3 ASP.NET Web API中的异常处理
原文:[ASP.NET Web API教程]4.3 ASP.NET Web API中的异常处理 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...
- 【ASP.NET Web API教程】4.1 ASP.NET Web API中的路由
原文:[ASP.NET Web API教程]4.1 ASP.NET Web API中的路由 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. ...
- 目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的创建
目标HttpController在ASP.NET Web API中是如何被激活的:目标HttpController的创建 通过上面的介绍我们知道利用HttpControllerSelector可以根据 ...
- ASP.NET Web API中的Routing(路由)
[译]Routing in ASP.NET Web API 单击此处查看原文 本文阐述了ASP.NET Web API是如何将HTTP requests路由到controllers的. 如果你对ASP ...
随机推荐
- AngularJs 返回上一页
html <script src="lib/angular/angular-1.4.9/angular.js"></script> <script s ...
- 工作中遇到的小问题: 做弹幕从数据库取出东西均匀插入marquee中,
function getFloatContent() { var method = 'GETFLOATCONTENT'; $.ajax({ url: 'api/zhenqiapi.php', data ...
- 20145225《Java程序设计》 第4周学习总结
20145225<Java程序设计> 第4周学习总结 教材学习内容总结 第六章 继承与多态 6.1继承 继承共同行为:存在着重复,可把相同的程序代码提升(pull up)为父类.exten ...
- Oracle中的带参数的视图--我们致力于打造人力资源软件
创建包和包体 create or replace package pkg_pv is procedure set_pv(pv varchar2); function get_pv return var ...
- 巧用Javascript中的slice()
slice()是Javascript中Array的一个方法,定义是这样的. arrayObject.slice(start,end)作用是从原数组中从start到end位置截取出一个新的数组,返回值是 ...
- Struts2的值栈和对象栈
ValueStack 如何得到值栈: 如何将对象存入值栈: 让值栈执行表达式来获得值: 在JSP中跳过栈顶元素直接访问第二层: 在JSP中访问值栈对象本身(而不是它们的属性) ActionContex ...
- JSON和js对象之间的相互转化
jQuery插件支持的转换方式 $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 http://www. ...
- sql行列转换
首先我们建立一张表,名为scoreInfo,各个字段的设计如下图,分别是name,course,score,表示姓名,成绩与分数,如图所示.
- java中的内部类小结
内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类.如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动).显然, ...
- node应用场景
2.1 Web开发:Express + EJS + Mongoose/MySQL express 是轻量灵活的Nodejs Web应用框架,它可以快速地搭建网站.Express框架建立在Nodejs内 ...