Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET Web API消息处理程序_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

C# ASP.NET Web API消息处理程序

C# ASP.NET Web API消息处理程序,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我想实现我的自定义消息处理程序,它将检查每个请求中必须存在的自定义头 如果存在我的自定义标头,则请求将通过,如果标头不存在,则将命中控制器。请求将被拒绝,并显示自定义错误消息 不,我的问题是:如果我以这种方式实现我的处理程序,这意味着所有请求都必须有头,但是我需要有一个门,在那里我可以在没有头的情况下调用,并且消息处理程序必须忽略该请求,即使没有自定义头,也必须命中控制器 有可能做到这一点吗?或者,我如何实现忽略特定控制器的某些调用的消息处理程序或类似的东西…?您可以尝试一下。。(未经测试) 在

我想实现我的自定义消息处理程序,它将检查每个请求中必须存在的自定义头

如果存在我的自定义标头,则请求将通过,如果标头不存在,则将命中控制器。请求将被拒绝,并显示自定义错误消息

不,我的问题是:如果我以这种方式实现我的处理程序,这意味着所有请求都必须有头,但是我需要有一个
门,在那里我可以在没有头的情况下调用,并且消息处理程序必须忽略该请求,即使没有自定义头,也必须命中控制器

有可能做到这一点吗?或者,我如何实现忽略特定控制器的某些调用的消息处理程序或类似的东西…?

您可以尝试一下。。(未经测试)


在这种情况下,您可能会使用授权筛选器或特定于路由的消息处理程序。我看不出这门操作可以完成。因为如果您在“http处理程序”或“http模块”或“操作筛选器”中对自定义头执行此检查。它将为所有呼叫运行。也许你可以添加另一个条件,比如如果某个查询字符串或cookie存在,那么不使用自定义标题就可以了。事实上@KiranChalla是对的,你可以实现路由特定的消息处理程序,如下所示:我认为这是一个复杂的方法来处理用户要求的内容。我听到了。提供一个简单的(r)响应,我将投票表决!我完全赞成学习新东西。
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);
    }


}