Oli Warner About Contact Subscribe

URLRewritingNet .* DefaultPage Hack

Thursday, 30 November 2006 technology

I think I’ve just cured the disease that’s been ailing me ever since I started using URLRewritingNet and just in case anyone is being effected the same way, I have the code that will allow you to fix the problem.

I’ll be honest with you from the off: I’m not 100% sure what implications this “hack” might have on the reliability of the rewriting, but if I’m right, this should only catch the request that has no regexes to catch it, namely the default document. The URL: “/”.

When doing standard URLRewriting, everything works fine but as soon as you start catching all URLs and sending the requests through ASPNET (something you need to do for fancy-pants URL rewriting like we’re using here), Default Documents no longer work and using the “/” URL with URLRewriting causes a NullReferenceException.

This is a documented problem and they’ve since added a DefaultPage attribute in the XML configuration that allows you to specify a page to redirect do (opposed to rewriting) meaning that people see your extensions. =(

You need to download the source from the URLRewritingNet website and open up the project. Inside there you will find a file called UrlRewriteModule.cs and (roughly) on line 309, you will see this:

app.Response.Redirect(sb.ToString());

sb is a StringBuilder that has been used to concat the default page, server name and a lot of other junk into forging a URL to redirect people to. Comment out that line and put this one in:

app.Context.RewritePath(UrlRewriting.Configuration.DefaultPage, false);

So you need to make sure that you have a DefaultPage (that is checked for earlier in the block of code) but more importantly, you need to make sure it really exists or it will explode like a bomb. Either way, I hope this helps.