1
Vote

Provide a way to replace the default HttpControllerDispatcher globally when hosted under ASP.NET

description

Assume that I have the following custom HttpControllerDispatcher implementation:
public class MyCustomDispatcher : HttpControllerDispatcher {

    protected override Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, 
        CancellationToken cancellationToken) {

        // Do your stuff here

        // According to your requirements, either run its default funtionality 
        // or return your own stuff
        return base.SendAsync(request, cancellationToken);
    }
}
And I want to register this globally. I thought that the best way would be to replace the GlobalConfiguration.DefaultHandler but it turns out that that property is read-only. so, the below solution is not applicable.

protected void Application_Start(object sender, EventArgs e) {
GlobalConfiguration.DefaultHandler = new HttpRoutingDispatcher(
    GlobalConfiguration.Configuration, new MyCustomDispatcher());
}

It's easy to do it per-route by attaching the handler but I wasn't able to find a way to do it globally.

This issue is also discussed under http://aspnetwebstack.codeplex.com/discussions/400366.

comments

HongmeiG wrote Apr 6 at 1:14 AM

Nice suggestion on getting things more extensible.