0

すべてのリクエストに存在する必要があるカスタム ヘッダーをチェックするカスタム メッセージ ハンドラーを実装したいと考えています。

カスタムヘッダーが存在する場合、リクエストは通過し、ヘッダーが存在しない場合はコントローラーにヒットし、カスタムエラーメッセージでリクエストが拒否されます。

いいえ、私の質問は次のとおりです。ハンドラーをそのように実装する場合、すべてのリクエストにヘッダーが必要ですが、gateそのヘッダーなしで呼び出すことができる場所が必要であり、リクエストはメッセージハンドラーによって無視され、カスタムがなくてもコントローラーにヒットする必要がありますヘッダ。

これを達成することは可能ですか?または、特定のコントローラーまたはこのようなものへの特定の呼び出しを無視するメッセージハンドラーをどのように実装できますか...?

4

1 に答える 1

0

これを試すことができます..(未テスト)

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);
    }


}
于 2013-10-22T19:25:40.820 に答える