为什么DelegatingHandler没有按路线开火?

时间:2017-04-04 04:29:28

标签: 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(
            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 static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();


            config.Routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "api/{controller}/{id}",
               defaults: new { id = RouteParameter.Optional }                 
           );
            config.Routes.MapHttpRoute(
               name: "SecureApi",
               routeTemplate: "secure/{controller}/{id}",
               defaults: new { id = RouteParameter.Optional },
               constraints: null,
               handler: new JwtCookieHandler()
           );
}

在Web API上:

public class DataController : ApiController
    {
     [Route("secure/data/abc")]
        [HttpGet]
        public HttpResponseMessage ABC()
        {
            return new HttpResponseMessage()
            {
                Content = new StringContent("hello");
            };
        }
}

如果我致电https://example.com/secure/data/abc =&gt;它显示了api数据但从不调用JwtCookieHandler。我做错了什么?

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

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

0 个答案:

没有答案