I've got an ASP.NET MVC Web API controller:
[HttpPost]
public async Task<HttpResponseMessage> Post(HttpRequestMessage req, CancellationToken cancellationToken) {...}
And a custom message handler I created:
public class MyMessageHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// ...
var response = await base.SendAsync(request, cancellationToken);
// ...
return response;
}
}
And in WebConfigApi.cs
I wire up the message handler route-specific to the controller action method:
configuration.Routes.MapHttpRoute(
name: "UpdateStuffAPI",
routeTemplate: "api/updatestuff/post/{stuffid}",
defaults: new { feedid = RouteParameter.Optional },
constraints: null,
handler: new MyMessageHandler()
);
When I POST to the controller action method, e.g.:
http://hostname/api/updatestuff/post?stuffid=12345
The message handler intercepts the request as expected. But in stepping through the line:
var response = await base.SendAsync(request, cancellationToken);
the controller action method is never hit.
As a test I removed the route-specific wiring and made the message handler global:
configuration.MessageHandlers.Add(new MyMessageHandler());
and SendAsync
properly invokes my controller's action method.
So my thought was that something is wrong with the route definition. However, the message handler is invoked with the route-specific wiring, and, Route Debugger shows that when I POST to my controller (http://hostname/api/updatestuff/post?stuffid=12345
), that that route is being used.
Why isn't my action method being invoked when I wire up the message handler in a route-specific way?