当前位置: 移动技术网 > IT编程>开发语言>.net > WebApiClient的接口输入验证方法

WebApiClient的接口输入验证方法

2018年08月08日  | 移动技术网IT编程  | 我要评论

龙袍打一字,中华人民共和国国歌,anmite

1. 文章目的

随着 webapiclient 的不断完善,越来越多开发者选择webapiclient替换原生的httpclient,本文将介绍webapiclient的接口参数输入有效性验证的新特性。

2.dataannotations介绍

asp.net mvc 服务端编程中,我们在创建模型的时候,使用system.componentmodel.dataannotations相关的验证特性,配合mvc框架,可以做前端和后端双向输入验证的效果。

public class userinfo
{
 [required]
 [stringlength(10, minimumlength = 1)]
 public string account { get; set; }

 [required]
 [stringlength(10, minimumlength = 6)]
 public string password { get; set; }
}

以上的required就是验证特性, asp.net mvc 在模型绑定的时候,会进行验证一遍,验证结果放在控制器的modelstate属性里面。当然system.componentmodel.dataannotations并不是 asp.net mvc 特有的,而是基础库自带的,也就是说任何框架下都是可以使用的。

3. 接口参数值的输入验证

validator静态类提validateobject相关的方法,用于验证实例和实例的属性值,webapiclient使用validator类来完成接口方法的参数值输入验证:

/// <summary>
/// 提供参数值和参数的属性值输入合法性验证
/// </summary>
static class parametervalidator
{
 /// <summary>
 /// 类型的属性否需要验证缓存
 /// </summary>
 private static readonly concurrentcache<type, bool> cache = new concurrentcache<type, bool>();

 /// <summary>
 /// 返回是否需要进行属性验证
 /// </summary>
 /// <param name="instance">实例</param>
 /// <returns></returns>
 private static bool isneedvalidateproperty(object instance)
 {
  if (instance == null)
  {
   return false;
  }

  var type = instance.gettype();
  if (type == typeof(string) || type.gettypeinfo().isvaluetype == true)
  {
   return false;
  }

  return cache.getoradd(type, t => t.getproperties().any(p => p.canread && p.isdefined(typeof(validationattribute), true)));
 }

 /// <summary>
 /// 验证参数值输入合法性
 /// 验证参数的属性值输入合法性
 /// </summary>
 /// <param name="parameter">参数描述</param>
 /// <param name="validateproperty">是否验证属性值</param>
 /// <exception cref="validationexception"></exception>
 public static void validate(apiparameterdescriptor parameter, bool validateproperty)
 {
  var name = parameter.name;
  var instance = parameter.value;

  foreach (var validation in parameter.validationattributes)
  {
   validation.validate(instance, name);
  }

  if (validateproperty == true && isneedvalidateproperty(instance) == true)
  {
   var ctx = new validationcontext(instance) { membername = name };
   validator.validateobject(instance, ctx, true);
  }
 }
}

4.接口参数的dataannotations声明

4.1 声明参数值的验证

例如getbyidasync方法有个id的参数,服务器要求必填且最大长度为10的字符串,我们可以使用required, stringlength(10)特性修饰id这个参数,在接口调用时,webapiclient会对id值进行验证,如果不通过则抛出validationexception的异常。

// /get webapi/user/getbyid?id=id001
// return httpresponsemessage
[httpget("webapi/user/getbyid/{id}")]
[basicauth("username", "password")]
itask<httpresponsemessage> getbyidasync(
 [required, stringlength(10)] string id);

4.2 声明参数值的属性验证

对于自定义的模型类型,只要在属性里声明了相关的dataannotations,webapiclient就自动进行属性的输入验证。

public class userinfo
{
 [required]
 [stringlength(10, minimumlength = 1)]
 public string account { get; set; }

 [required]
 [stringlength(10, minimumlength = 6)]
 public string password { get; set; }
}

// post webapi/user/updatewithjson
// body {"account":"laojiu","password":"123456"}
// return json或xml内容
[httppost("webapi/user/updatewithjson")]
itask<userinfo> updatewithjsonasync(
 [jsoncontent("yyyy-mm-dd hh:mm:ss")] userinfo user);

当user参数不为null的情况,就会验证它的account和password两个属性。

4.3 声明参数值、参数的属性值同时验证

对于4.2的例子,如果我们希望user参数值也不能为null,可以如下声明方法:

// post webapi/user/updatewithjson
// body {"account":"laojiu","password":"123456"}
// return json或xml内容
[httppost("webapi/user/updatewithjson")]
itask<userinfo> updatewithjsonasync(
 [required][jsoncontent("yyyy-mm-dd hh:mm:ss")] userinfo user);

5. 禁用参数的属性验证

如果你的模型的属性已声明验证特性,但不希望webapiclient进行属性值验证,可以在创建接口实例的时候,在配置项里禁用属性验证:

var config = new httpapiconfig
{
 useparameterpropertyvalidate = false
};
var client = httpapiclient.create<iuserapi>(config);

6. 结束语

博主为webapiclient库的作者,本文向读者介绍了dataannotations验证特性在webapiciient下的使用方法,欢迎大家给webapiclient提建议。 也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网