2

I cannot get the WebAPI DelegatingHandler to execute based on the route

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

On Web api registration:

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

On Web API:

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

if I call https://example.com/secure/data/abc => it shows the api data but never calls JwtCookieHandler. What did I do wrong ?

If I set it to work globally

config.MessageHandlers.Add(new JwtCookieHandler()); => invoke the handler for every api call
nam vo
  • 3,271
  • 12
  • 49
  • 76
  • I guess I have problems with how to call the proper api name, rather than the delegatingHandler. – nam vo Apr 04 '17 at 05:27
  • Update handler to inspect request url for some prefix that satisfies desired route. you are having issues because you don't fully understand attribute routing and convention based routing. You could also consider using action filter or middleware. – Nkosi Apr 04 '17 at 12:29

0 Answers0