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
原创粉丝点击