Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you can do is use the per-route message handler but be careful here. As the article that @Nick linked in his answer, you can chain the handler and ensure that the <code>HttpControllerDispatcher</code> is involved. Otherwise, you won't get into the Controller pipeline.</p> <p>One other option which I like is to use the <code>HttpControllerDispatcher</code> as a base class for your custom handler:</p> <pre><code>public class CustomerOrdersDispatcher : HttpControllerDispatcher { public CustomerOrdersDispatcher(HttpConfiguration config) : base(config) { } protected override Task&lt;HttpResponseMessage&gt; SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { // Do some stuff here... return base.SendAsync(request, cancellationToken); } } </code></pre> <p>Here, how you will register it:</p> <pre><code>protected void Application_Start(object sender, EventArgs e) { var config = GlobalConfiguration.Configuration; config.Routes.MapHttpRoute( name: "CustomerOrdersHttpRoute", routeTemplate: "api/customers/{customerKey}/orders/{key}", defaults: new { controller = "CustomerOrders", key = RouteParameter.Optional }, constraints: null, handler: new CustomerOrdersDispatcher(config) ); config.MessageHandlers.Add(new SomeOtherHandler1()); config.MessageHandlers.Add(new SomeOtherHandler2()); } </code></pre> <p>After <code>SomeOtherHandler1</code> and <code>SomeOtherHandler2</code> are executed, your <code>CustomerOrdersDispatcher</code> will be executed for <strong>CustomerOrdersHttpRoute</strong> route. So, you can see that you preserve the default handler behavior and set some global handlers while you have a route specific one as well.</p> <p>Here is the full implementation of my <code>CustomerOrdersDispatcher</code>: <a href="https://github.com/tugberkugurlu/AdvancedWebAPI/blob/master/PerRouteMHOwnershipSample/Dispatcher/CustomerOrdersDispatcher.cs" rel="noreferrer">https://github.com/tugberkugurlu/AdvancedWebAPI/blob/master/PerRouteMHOwnershipSample/Dispatcher/CustomerOrdersDispatcher.cs</a>. </p> <p>You may view the full sample application source code as well to see how it works out: <a href="https://github.com/tugberkugurlu/AdvancedWebAPI/tree/master/PerRouteMHOwnershipSample" rel="noreferrer">https://github.com/tugberkugurlu/AdvancedWebAPI/tree/master/PerRouteMHOwnershipSample</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload