Last post Apr 01, 2014 02:25 AM by damienBod
Mar 28, 2014 11:21 AM|bitdiff|LINK
I am implementing an api with credentials implemented via a DelegatingHander which is working perfectly.
I register the handler globally using something like (where container is my DI container):
The issue I have is I have a single controller that I want to opt-out of this handler. I am currently using AttributeRouting so I don't have the opportunity to register the message handler per-route.
I thought I could create a new attribute to apply to the one controller to opt-out, using something like:
public class DisableApiKeyHandlerAttribute : Attribute, IControllerConfiguration
public void Initialize(HttpControllerSettings controllerSettings,
However, this clears out the handlers for all controllers (I assume because the configuration attached to the controllerDescriptor instance is a reference shared between all controllers).
Is this by design or am I missing something obvious? Is there a way to opt-out of a global handler for a single handler using attribute routing?
Mar 29, 2014 05:03 AM|damienBod|LINK
You can add the MessageHandler to the route configuration pattern and not the Global config. Then create a specific route for the controller where this should not be applied.
Here's an example:
handler: new VersioningHandlerReturn()
Mar 31, 2014 04:46 AM|bitdiff|LINK
I can't get this to work while using attribute routing.
Or the other way around:
In either case, it doesn't seem like you can have a combination of attribute routing and normal routing configuration in this scenario unless I am doing something wrong?
Apr 01, 2014 02:25 AM|damienBod|LINK
You have added your MessageHandler to Global configuration. This means it will always be called.
You could add an exception rule in the MessageHandler itself as a workaround. Here's 2 ways of checking the route data or the URI.
var routes = HttpContext.Current.Request.RequestContext.RouteData;
public class ApiKeyHandler: DelegatingHandler
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
var response = await base.SendAsync(request, cancellationToken);
// SKIP you global logic
You could then exclude the controller.
hope this helps