Set your IHttpHandlers using RouteTable rather than web.config

If for whatever reason you use IHttpHandlers in your MVC project, it may be that you are still using web.config to set the path to the handlers. This can get tricky and easy to miss, especially if you move from cassini dev to IIS7 live.

There is however a better, neater way to declare paths to your handers. If you are using MVC, you can register the handlers in RouteTable, along with your other routes. All you need to do is implement IRouteHandler interface and register a route using your custom route handler.

Furthermore, you can add tokens on the route while you’re registering the handlers to the RouteCollection. These can be identified by the routehandler and passed down to the handler as arguments.

Lets assume you have a handler that looks after your file upload. The same handler is re-used for multiple different file upload processes and we need to be aware, which upload type it is. The handler should be registered at ~/handlers/uploadfile/{type}

First lets create the FileUploadHandlerRoute

 1public class FileUploadHandlerRoute : IRouteHandler 
 3    public IHttpHandler GetHttpHandler(RequestContext requestContext)
 4    {
 5        var uploadType = ImageUploadType.Simple;
 6        var type = requestContext.RouteData.Values["type"].ToString();
 7        Enum.TryParse(type, out uploadType);
 9        return new FileUploadHandler(uploadType);
10    }

And then, simple add the handler route to your RouteCollection

1//..your other routes
4    "UploadFileHandler",
5    new Route("handlers/uploadfile/{type}", new FileUploadHandlerRoute())

If you like the post, have any questions or wish to shout some abuse follow me @mirajavora