WebAPI 添加模型验证
来源:互联网 发布:3g网络网速 编辑:程序博客网 时间:2024/05/09 20:44
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() });
}
}
}
}
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
- WebAPI 添加模型验证
- WebAPI权限验证
- WebApi实现验证授权Token,WebApi生成文档等
- WebApi实现验证授权Token,WebApi生成文档等
- WebAPI 传递复杂类型处理绑定模型
- WebApi安全性 使用TOKEN+签名验证
- WebApi安全性 使用TOKEN+签名验证
- WebApi安全性 使用TOKEN+签名验证
- WebApi安全性 使用TOKEN+签名验证
- FFOS gecko 添加javascript 实现的webapi
- 给现有MVC 项目添加 WebAPI
- WebAPI
- webApi
- webapi
- WebAPi
- webAPI
- webapi
- WebAPI
- 50个常用sql语句
- spring配置文件中的mappingResources、mappingLocations、mappingDirectoryLocations、mappingJarLocations区别
- leetcode: Remove Duplicates from Sorted Array
- Redis源码学习2-内存管理
- xsd转化为XML格式
- WebAPI 添加模型验证
- 避免Block中的强引用环
- Struts 1.x | <logic> 标签库
- Java中单例类和不可变类的理解
- Single Number
- php中抽象类abstruct和接口interface的共同点与不同点区别
- ASP之vbscript的HTML事件
- Jedis的Publish/Subscribe功能的运用
- Android: Run systrace under windows