I am looking for a way to intercept/grab the request being made before matching to a route. For example, I have multiple controllers and routes set up, but I want some mecha
I'm using mentioned technique to log all requests and responses. Speaking shortly, the best way to do it is to use Handlers.
First, create handler:
public class CustomHandler : DelegatingHandler
{
protected override async Task SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
//get route values and process them
var routeValues = (IHttpRouteData[]) HttpContext.Current.Request.RequestContext.RouteData.Values["MS_SubRoutes"];
//let other handlers process the request
return await base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
//once response is ready, do something with it
return task.Result;
}, cancellationToken);
}
}
Then, register it in WebApiConfig:
config.MessageHandlers.Add(new CustomHandler());