I'm trying to add a route to my WebAPI with a custom message handler like so
config.Routes.MapHttpRoute(
name: "TestRoute",
routeTemplate: "Protected/TestMe",
defaults: null,
constraints: null,
handler: new CustomHandler()
);
Handler code
internal class CustomHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage respMsg = new HttpResponseMessage();
respMsg.Headers.Add("MyCustomHeader", "Test");
respMsg.StatusCode = HttpStatusCode.NoContent;
return Task.FromResult(respMsg);
}
}
but everytime I access said route I am not getting my custom header back or teh NoContent code instead I'm receiving the result from route, but if I add the handler as a global one as this
config.MessageHandlers.Add(new CustomHandler());
it works and returns my custom header and the NoContent status code What am I missing in the route setup?