Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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# 为什么DelegatingHandler不按路线开火?_C#_Asp.net_Asp.net Web Api_Delegatinghandler - Fatal编程技术网

C# 为什么DelegatingHandler不按路线开火?

C# 为什么DelegatingHandler不按路线开火?,c#,asp.net,asp.net-web-api,delegatinghandler,C#,Asp.net,Asp.net Web Api,Delegatinghandler,我无法使WebAPI DelegatingHandler基于路由执行 public class JwtCookieHandler : DelegatingHandler { public static string SessionIdToken = "session-id"; async protected override Task<HttpResponseMessage> SendAsync( HttpRequ

我无法使WebAPI DelegatingHandler基于路由执行

  public class JwtCookieHandler : DelegatingHandler
    {
        public static string SessionIdToken = "session-id";

        async protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        { 
            //blah blah////////////////
            HttpResponseMessage response = await base.SendAsync(request, 
            cancellationToken);

            // Set the session ID as a cookie in the response message.
            response.Headers.AddCookies(new CookieHeaderValue[] {
            new CookieHeaderValue(SessionIdToken, sessionId)  });

            return response;
         }
在Web API上:

public class DataController : ApiController
    {
     [Route("secure/data/abc")]
        [HttpGet]
        public HttpResponseMessage ABC()
        {
            return new HttpResponseMessage()
            {
                Content = new StringContent("hello");
            };
        }
}
如果我调用=>它将显示api数据,但从不调用JwtCookieHandler。我做错了什么

如果我将其设置为全球工作

config.MessageHandlers.Add(new JwtCookieHandler()); => invoke the handler for every api call

我想我在如何调用正确的api名称,而不是delegatingHandler.Update处理程序来检查请求url中满足所需路由的前缀方面遇到了问题。您遇到问题是因为您不完全了解属性路由和基于约定的路由。您还可以考虑使用Actudio过滤器或中间件。
config.MessageHandlers.Add(new JwtCookieHandler()); => invoke the handler for every api call