Web API 實戰之 統一的請求驗證

來源:互聯網
上載者:User

篩選設定:

<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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.