Adventures of a wannabe geek!

Ranting within

IIS Rewrite Module and ASP.NET MVC3

This morning I found out that the IIS Rewrite module causes a problem with MVC routing when trying to POST back to the server. I added a rule to the web.config to enforce all lowercase URLs as follows:


As you can see this is the rule that comes out of the box with the module. There doesn’t seem to be any notes included with this particular rule. This adds the following to the web.config

    <modules runAllManagedModulesForAllRequests="true"></modules>
    <remove name="UrlRoutingHandler" />
            <rule name="LowerCaseRule1" stopProcessing="true">
                <match url="[A-Z]" ignoreCase="false" />
                <action type="Redirect" url="{ToLower:{URL}}" />

This works as expected as it changes http://localhost/SiteName/Account/Register  to http://localhost/sitename/account/register. We have decided to keep all URL’s lowercase and enforce the rule so Google cannot see duplicate URLs due to casing. When the user is presented with the register view, a form on that page has a POST action. When that POST action posts to a non-lowercase URL then the Rewrite module weighs in a redirects to the lowercase URL and presents the user with the view again and not with the results of the POST.

I mentioned this on twitter and Phil Winstanley, of Microsoft, kindly offered to enquire about this on the internal distribution lists to see what that brought back. The responses helped me understand what actually happen. they also pointed out that the behaviour, although it isn’t strictly correct, is kind of expected. According the HTTP spec

“If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request
unless it can be confirmed by the user, since this might change the conditions under which the request was issued.“

It was also pointed out to me that as Login and Register are not really sites that are promoted for SEO purposes then we can exclude these routes from the redirect rule. Overall I have seen that I have not got a flawless plan for enforcing lowercase URLs. If I want lowercase URLs then I should really enforce them myself in my code. The one thing I’m slightly confused about is that there are no notes that come with the IIS Rewrite module that tell me these types of things. Fair enough I can get them from HTTP spec, but I really didn’t realise that this would be an issue.

If possible, I’d love people to see that the Rewrite module documentation may be lacking in specific areas. Hopefully this will improve. I’d like to thank Phil and all the guys at Microsoft who helped with responses in this query.