一、WebApi模型验证实践项目使用

一、启语
前面我们说到,模型验证的原理(包含1、项目创建,2、模型创建,3、走通测试模型验证,4、在过滤器中处理返回json格式(非控制器内))-完全是新手理解使用的,新番理解

  通常情况下,对于那些经常为别人提供数据接口的开发人员来说,对于调用方传递过来的参数都会有验证处理。例如:

if (string.IsNullOrEmpty(entity.Name))
{
    //当姓名为空时,.........
}
if (entity.Age<0 || entity.Age>100)
{
    //当年龄小于0或大于100时,有的人可能超过一百岁,我希望我的有生之年Age>100,emm,.........
}
 
if (string.IsNullOrEmpty(entity.PhoneNum))
{
    //当电话号码为空时,.........
}
if (!string.IsNullOrEmpty(entity.PhoneNum))
{
    Regex regex = new Regex("^1[34578]\\d{9}$");
    var result=regex.IsMatch(entity.PhoneNum);
    if (!result)
    {
        //当电话号码格式不合法时,.........
 
    }
}
//以下还有50个字段需要验证,是不是有种绝望的感觉,有木有?有木有?有人可能会跟调用者说,某个字段必须怎么怎么样,有些人就喜欢故意传错,故意刁钻,到头来,最苦逼的还是写接口的人。

 有时候一个系统有十几二个接口,光验证的代码就一大堆,真的有时候不想写,都说程序猿是最聪明的一批人,自然有好的解决方法。

  C# webapi 有一种叫模型验证的东西,在java里面,应该有一种注解的东西可以处理。

  在ASP.NET Web API中,你可以使用System.ComponentModel.DataAnnotations命名空间的注解属性来设置模型属性的验证规则。考虑以下模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace WebApi.Models
{
    public class Personnel
    {
        [Required(ErrorMessage = "Name参数不能为空")]//Required 验证这个参数不能为空 ErrorMessage:为空时自定义错误信息
        public string Name { get; set; }
        [Required(ErrorMessage = "Age参数不能为空")]
        [Range(1, 100, ErrorMessage = "Age参数只能是大于1,小于100")]//Range 验证值只能在某些范围内,只适用于Int类型的字段
        public int Age { get; set; }

        [Required(ErrorMessage = "电话号不能为空")]
        [RegularExpression("^[1]+[3,4,5,7,8]+\\d{9}", ErrorMessage = "PhoneNum不合法的电话号码格式")]//RegularExpression 用正则验证字段的合法性,多用于身份证、电话号码、邮箱、等等...
        public string PhoneNum { get; set; }
    }
}

 

这样的话就可以节省大量冗余代码,那么,接下来如何去处理呢?

 在接口请求之前,我们可以对客户端传过来的数据进行模型验证处理,对于webapi比较熟悉的人应该知道过滤器,不知道的可以看我前面的文章,如何使用webapi三大过滤器,过滤器是基于AOP,面向切面的编程思想。

 在这里我们用的ActionFilterAttribute过滤器,只要继承这个接口并且实现其中的OnActionExecuting方法就行。具体实现如下:

方式一:
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Web.Http.ModelBinding;

namespace WebApi.Filters
{
    public class ValidateModelAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext.ModelState.IsValid == false)
            {
                // Return the validation errors in the response body.
                // 在响应体中返回验证错误信息
                var errors = new Dictionary<string, IEnumerable<string>>();
                foreach (KeyValuePair<string, ModelState> keyValue in actionContext.ModelState)
                {
                    errors[keyValue.Key] = keyValue.Value.Errors.Select(e => e.ErrorMessage);
                }
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    code = HttpStatusCode.BadRequest,//返回客户端的状态码
                    success = false,
                    error = errors//显示验证错误的信息
                });
            }
        }
    }
}
方式二:

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Web.Http.ModelBinding;

namespace WebApi.Filters
{
    public class ValidateModelAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext.ModelState.IsValid == false)
            {
                //如果验证是吧,只取第一个错误信息返回
                //var item = actionContext.ModelState.Values.Take(1).SingleOrDefault();
                //var errors = item.Errors.Where(b => !string.IsNullOrWhiteSpace(b.ErrorMessage)).Take(1).SingleOrDefault().ErrorMessage;
                
                //直接获取
                var error=actionContext.ModelState.Values.First().Errors.First().ErrorMessage ;
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    code = HttpStatusCode.BadRequest,//返回客户端的状态码
                    success = false,
                    error = error//显示验证错误的信息
                });
            }
        }
    }
}

 

 

 

注意:Model必须不能为null,正则的验证,必须满足必选项(即有值才会触发正则进行验证-一般前端键名肯定会传的,除非恶意,只请求,而不传参出现这种情况,我这边也要想办法处理 null)。

 

 //

页面可以参考

https://www.cnblogs.com/fonour/p/5943401.html

 

 

转载于:https://www.cnblogs.com/fger/p/11239246.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值