WebAPI 添加模型验证

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Filters;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using Legal.Api.Controller;
using System.Reflection;
/***********************************************************  
** 作者: dl 
** 创始时间:2014年5月09日
** 描述:
** 对HTTP 请求 模型验证过滤类
************************************************************/
namespace Legal.Api.Service
{
    public class ModelValidationFilterAttribute : ActionFilterAttribute
    {
      
       
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            try
            {
                if (actionContext == null)
                {
                    ErrorLogCom.WriteErrorLog("【手机服务端错误】Message: HttpActionContext对象为空!");//写入错误日志
                    actionContext.Response = actionContext.Request.CreateResponse<ErrorResult>(HttpStatusCode.OK, new ErrorResult { Code = CodeEnum.数据包为空, Message = CodeEnum.数据包为空.ToString() }); 
                    return;
                }
                //var auth = actionContext.Request.Headers.Authorization; //用作客户端授权用
                //if (auth==null)
                //{
                //    actionContext.Response = actionContext.Request.CreateResponse<ErrorResult>(HttpStatusCode.OK, new ErrorResult { Code = CodeEnum.调用接口校验不成功, Message = CodeEnum.调用接口校验不成功.ToString() });
                //    return;
                //}
                //验证模型验证是否通过
                if (actionContext.Request.Method != HttpMethod.Get)
                {
                    if (!actionContext.ModelState.IsValid)
                    {
                        var errors = new Dictionary<string, IEnumerable<string>>();
                        actionContext.ModelState.ToList().ForEach(p =>
                        {
                            errors[p.Key] = p.Value.Errors.Select(e => e.ErrorMessage);
                        });
                        //var errors = new HttpError(actionContext.ModelState,true);
                        actionContext.Response = actionContext.Request.CreateResponse<JsonResult>(HttpStatusCode.OK, new JsonResult { Code = CodeEnum.解析报文出错, Result = errors });
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLogCom.WriteErrorLog("【手机服务端错误】Message:" + ex.Message + ",StackTrace:" + ex.StackTrace); //这里是些错误日志
                actionContext.Response = actionContext.Request.CreateResponse<ErrorResult>(HttpStatusCode.BadRequest, new ErrorResult { Code = CodeEnum.调用接口校验不成功, Message = CodeEnum.调用接口校验不成功.ToString() });
            }


        }


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值