簡體   English   中英

ASP.NET Web API消息處理程序

[英]ASP.NET Web API message handler

我想實現我的自定義消息處理程序,該處理程序將檢查每個請求中必須存在的自定義標頭。

如果存在我的自定義標頭,則請求將通過,如果標頭不存在,則將命中控制器,請求將被拒絕,並顯示自定義錯誤消息。

沒有我的問題是:如果我實現了我的處理程序的方式,這意味着所有的請求必須有標題,但是我需要有一個gate ,我可以不說頭打電話,請求必須通過消息處理程序被忽略,並沒有連打控制器自定義標題。

有可能實現這一目標嗎? 或如何實現我的消息處理程序,該消息處理程序將忽略對特定控制器的某些調用或諸如此類的...?

你可以試試這個。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;


 public abstract class EnforceMyBusinessRulesController : ApiController
{

    protected override void Initialize(System.Web.Http.Controllers.HttpControllerContext controllerContext)
    {

        /*

            Use any of these to enforce your rules;

            http://msdn.microsoft.com/en-us/library/system.web.http.apicontroller%28v=vs.108%29.aspx

            Public property Configuration   Gets or sets the HttpConfiguration of the current ApiController.
            Public property ControllerContext   Gets the HttpControllerContext of the current ApiController.
            Public property ModelState  Gets the model state after the model binding process.
            Public property Request Gets or sets the HttpRequestMessage of the current ApiController.
            Public property Url Returns an instance of a UrlHelper, which is used to generate URLs to other APIs.
            Public property User    Returns the current principal associated with this request. 
        */

        base.Initialize(controllerContext);

        bool iDontLikeYou = true; /* Your rules here */
        if (iDontLikeYou)
        {
            throw new HttpResponseException(new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.NotFound));
        }


    }

}



public class ProductsController : EnforceMyBusinessRulesController
{

    protected override void Initialize(System.Web.Http.Controllers.HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);
    }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM