28.Apr.17

Vladimir Milev

Vladimir Milev

Poor man's URL Rewriter for ASP.NET

So you need to do some URL rewrites in a web project of yours to support some backwards compatibility. The right thing to do, is of course to not reinvent the wheel, but to use the insanely good URL Rewrite module that comes with IIS. It has a broad range of functionality that has the flexibility to handle a whole lot of scenarios.

If you have more complex redirection patterns you can use the powerful regex support in the module. Sometimes, however, things can be more trivial and you simply want to map one specific URL to another specific URL. That’s when you want to use the RewriteMaps functionality of the same module.

So wait a minute. It seems to me that the URL Rewrite module covers it all, why would I possibly want to write up my own one? That’s a very good question, and indeed the answer to it in 99% of the cases would be to NOT write your own. However, there are always exceptions. One such exception is when you have so much redirects to process that you simply go over the maximum configuration file size (250KB by default).

There are several ways to deal with this, the first being is to try and split the config file into several files. Actually, slap yourself on the cheek if you are not already doing this for neatness 😊. In some border cases, this will do the trick. If both files remain under the 250KB boundary you are good to go. However, sometimes even your dedicated rewrite.config will still be larger than the default limit. One option, of course, is to change the limit in the registry. But you will sometimes be unable to do this or unwilling. Examples can be a very restrictive IT policy which doesn’t allow you to do this. Or you are using Azure Web Apps in which case it is a pain in the butt to change registry entries. Or you simply don’t want to make life harder for non tech savvy customers who will be deploying the app instead of you.

At any rate there are sometimes when you simply just want to do some simple rewriting/redirecting just like the rewrite map, but without the strict limits. After searching a bit, I’ve decided to write up my own very simple poor man’s URL Rewriter that handles the standard RewriteMap.confing, so it is convenient to just pickup from where IIS left. Here is the code:

And as always, happy coding!

Need consulting on this topic?

Yes No

Thank You! We will be in touch.