How can I restrict a delegating handler to a specific route in Web API?

Viewed 8177

I have a custom delegating handler that manages the authentication of a request. In one of my controllers, authentication should not be enabled for a specific action. How can I disable the delegating handler for the method and route POST api/MyController?

One option is to hard code the route inside the handler, however, I would rather keep this logic out of the handler. In addition, I see myself adding this behavior to a few more actions, which can make this method hard to maintain.

protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
    if (request.Method == HttpMethod.Post
        && request.RequestUri.PathAndQuery.StartsWith("/api/MyController"))
        return base.SendAsync(request, cancellationToken);

    // Do authentication
}

Is there a better way that's easier to maintain?

1 Answers
Related