Web API 实战之 统一的请求验证
来源:互联网 发布:远程授课软件 编辑:程序博客网 时间:2024/06/13 08:24
筛选器部分:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">using Ahoo<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Demo</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Message</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>using System<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>using System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Collections</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Generic</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>using System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Linq</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>using System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Net</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>using System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Net</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Http</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>using System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Web</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Http</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>using System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Web</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Http</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Controllers</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>using System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Web</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Http</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Filters</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>using System<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Web</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Http</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Properties</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>namespace Ahoo<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Demo</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.WebAPI</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Filter</span>{ /// <summary> /// 请求参数验证,统一处理筛选器 /// </summary> [AttributeUsage(AttributeTargets<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Method</span> | AttributeTargets<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Class</span>, Inherited = true, AllowMultiple = true)] public class ValidationMessageAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (!actionContext<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ModelState</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.IsValid</span>) { ResponseMessage resp = new ResponseMessage { IsSuccess = false, ErrorCode = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"0001"</span>, Message = actionContext<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ModelState</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Values</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.First</span>()<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Errors</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.ErrorMessage</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> actionContext<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Response</span> = actionContext<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Request</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.CreateResponse</span><ResponseMessage>(resp)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> } base<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.OnActionExecuting</span>(actionContext)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span> } }}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li></ul>
使用部分:
在Global 注册请求筛选器:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">GlobalConfiguration<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Configuration</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Filters</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Add</span>(new ValidationMessageAttribute())<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code>
0 0
- Web API 实战之 统一的请求验证
- Web API 实战之 统一的请求验证
- Web API 实战之 授权验证
- web统一登录验证
- RESTful的Api设计之统一接口
- Web API 实战之 异常处理
- ASP.NET Web API之FluentValidation验证
- 验证框架的统一
- yii2项目实战-restful api之授权验证
- springboot【19】日志管理之使用AOP统一处理Web请求日志
- 基于typeScript请求服务端的js-api-sdk统一封装思路
- LDAP统一验证的理解
- ZooKeeper实战应用之【统一配置管理】
- ZooKeeper实战应用之【统一配置管理】
- ZooKeeper实战应用之【统一配置管理】
- ZooKeeper实战应用之【统一配置管理】
- web开发-网络请求实战
- 基于自定义属性的Web客户端统一验证JavaScript函数库组件(ChkInputs)概述
- How to Deliver Proposals and Presentations
- 关于hibernate.cfg.xm
- Roman to Integer ——解题报告
- javascript表单的正则表达式
- 睡眠长短影响健康 多方面提高睡眠…
- Web API 实战之 统一的请求验证
- HTML5 LocalStorage 本地存储
- Loadrunner 9.5/11 java vuser环境配置问题(已解决)
- ACdream 1056 Vitaly and Strings
- 分享一篇GDB调试方法
- jeecg权限设计的介绍之数据权限
- org.hibernate.console.HibernateConsoleRuntimeException: Received a NoClassDefFoundError
- 子网掩码和ip地址的关系
- javascript call和apply方法详解