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