Best possible url format in asp.net mvc on IIS6
I been interested in asp.net mvc since its preview2 release but really its now that I am playing with it. I have downloaded the asp.net mvc preview4 and is currently enjoying the cool new method of developing asp.net websites.
An important feature of asp.net mvc framework is its pretty urls and ability to map incomming requests to controllers and action like other mvc frameworks. However the url format like “/blog/archive/hellow-world” would not work on iis6 by default. For these types of urls to work on iis, we either have to setup iis to route all incomming requests to asp.net or use a different url format.
Check out this blog post “Deploy ASP.NET MVC on IIS 6, solve 404, compression and performance problems” by Omar AL Zabir for a description of problems you will run into when deploying asp.net mvc application on iis6 and solutions for them. You may also want to check out this article on codeproject which uses the 404 page not found redirection to symulate an extension less url format.
The problem gets more bad if you are hosting your mvc site on a shared host as most of them will not allow making the changes required for routing all requests to the asp.net.
Bit ugly but simple solution which will work everywhere
The solution is to change the url format. Instead of having “/controller/action” we can have “/controller/action.aspx”. This way, all the urls will have .aspx as their extension and naturally be handled by asp.net. This work around also removes the need for handling those good things our self which IIS does for us.
Yes the url “/blog/archive.aspx” looks ugly, compared to the “/blog/archive” but may be the only simple solution for using asp.net mvc on a restricted shared host.
Code snippet
RouteTable.Routes.MapRoute(null, “{controller}.aspx”, new { controller = “home”, action = “index” });
RouteTable.Routes.MapRoute(null, “{controller}/{action}.aspx”, new { controller = “home”, action = “index” });
The above code snippet is from the application_start event. With this routing rules set, the requests like “/home.aspx” and”/home/about.aspx” will be handled correctly. Have not found any issue yet with this url style, will post if found.
All the best asp.net mvc!
If you liked this post, please subscribe to this blog or kick this story on DotNetKicks.com .
Tags: asp.net mvc, iis

August 16th, 2008 at 6:33 pm
[...] to Vote[Del.icio.us] Best possible url format in asp.net mvc on IIS6 (8/16/2008)Saturday, August 16, 2008 from [...]
August 16th, 2008 at 11:47 pm
Nice, this is the same thing I was just looking at doing yesterday.
Another thing I’m looking at doing is getting rid of the Home “folder”. I’d rather have my url’s be “/” instead of “/home.aspx” and “/about.aspx” instead of “/home/about.aspx”. I haven’t looked into it too much, but do you have any tips on doing this?
August 17th, 2008 at 11:02 am
Hi Chris,
The problem with that url format is the router will not be able to make it whether the request is for a controller with the name “about” or for an action “about” on “home” controller. After some playing, I found a work around rule
routes.MapRoute(”homepage”,
“{action}.aspx”,
new { controller = “home”, action = “index” },
new { action = @”index|about” });
This rule explicitly lists the actions which can be invoked like that using a constraint. if you add a new action to the home controller, you will have to modify the routing rule also to include the allowed actions.
To be able to use “/” for the default page, add the following rule
routes.MapRoute( null, “default.aspx”, new { controller = “home”, action = “index” });
and set the RoutExistingFiles to true. The complete snippet is below
routes.RouteExistingFiles = true;
routes.MapRoute(null, “”, new { controller = “home”, action = “index” }); // to get a “/” when generating the link for homepage automatically
routes.MapRoute(”homepage”,
“{action}.aspx”,
new { controller = “home”, action = “index” },
new { action = @”index|about” });
routes.MapRoute(”rootpage”, “default.aspx”, new { controller = “home”, action = “index” });
Enabling RouteExistingFiles can be troublesome if all the requests are being handled by asp.net, check this url
http://forums.asp.net/t/1263849.aspx
For routing constraints
http://weblogs.asp.net/stephenwalther/archive/2008/08/06/asp-net-mvc-tip-30-create-custom-route-constraints.aspx
Thanks for pointing towards this issue, Learned some new things solving it. let me know if you find any better solution.
Configuring routes can be a tricky thing!