ASP.NET MVC URLs in IIS 6 using Ionic ISAPI Rewrite Filter
Brian LeGros | July 24th, 2008 | programmingOk, so I am totally swiping this from Ben Scheirman's blog post, but I thought I'd consolidate my comments on his post into a summary post. I've been working with ASP.NET MVC for a month now and everything has worked out as expected when I run my applications from the built-in IIS instance in VS2008. When I deploy to my local development environment, however, I run into the well know URL issue associated with using IIS 6.0. I dug into scraping the interwebs and found Ben's post as well as a post from Steve Sanderson. Both posts did the trick to help me fix the problems I was experiencing. Please keep in mind, since this is a PoC project, I have no budget, but I was able to find a solution using OSS.
+1 Ionic Isapi Rewrite Filter
Here are the steps I went through to get things working:
- Install Ionic's ISAPI Rewrite Filter into IIS
- Download version the file IonicIsapiRewriter-1.2.14-bin.zip and unzip it.
- Copy the IsapiRewrite4.dll file under the /lib folder to C:\Windows\system32\inetsrv.
- Create a file named IsapiRewrite4.ini and copy it into the C:\Windows\system32\inetsrv folder.
- Use the following template for the INI file. This will be used to rewrite URLs to include a .mvc extension so that incoming HTTP requests will be routed to ASP.NET:
-
# empty URL gets mapped to home controller
-
RewriteRule ^/$ /home [R]
-
-
# needed for URLs one token deep
-
# map controller parts of urls to .mvc, ignoring the content directory
-
RewriteRule ^(?!/Content)(/[A-Za-z0-9_-]+)$ $1.mvc [I]
-
-
# needed for URLs more than one token deep
-
# map controller parts of urls to .mvc, ignoring the content directory
-
RewriteRule ^(?!/Content)(/[A-Za-z0-9_-]+)(/.*)?$ $1.mvc$2 [I]
NOTE: We need two rewrite rules to account for URLs one token deep that do not have a trailing slash. For example, if we excluded the 2nd rewrite rule, the url "/home" would be rewritten internally to "/home$2". I'm not sure if this is intended behavior (PCRE rules don't seem to agree when I try rule 3 here with the previous example), but adding the 2nd rewrite rule prevents this from happening.
-
- Under "Properties" on the website you've created in IIS (via the IISAdmin), select the "ISAPI Filters" tab, and click the "Add" button.
- Use "Ionic Rewriter" for the value of the "Name" field, browse to C:\Windows\system32\inetsrv\IsapiRewrite4.dll for the file, and click OK.
- Stop the IISAdmin service then start the World Wide Web Publishing Service on the machine.
- NOTE: There are more detailed installation instructions in the download with more options, these are just the steps I took.
- Create a mapping for the ".mvc" extension in IIS to instruct ASP.NET to service these HTTP requests.
- Under "Properties" on the web site you've created in IIS (via the IISAdmin), select the "Home Directory" tab, and then click the "Configuration" button.
- Under "Wildcard Application Maps" click the "Insert" button, browse to the file C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll for the value of the "Executable" field, uncheck the box "Verify File Exists", and click the "OK" button.
- Add the following method to your web application's globals.asax.cs file in the Globals class:
-
protected void Application_BeginRequest(Object sender, EventArgs e)
-
{
-
HttpApplication app = sender as HttpApplication;
-
if (app != null)
-
{
-
if (app.Request.AppRelativeCurrentExecutionFilePath.Contains(".mvc"))
-
{
-
app.Context.RewritePath(app.Request.Url.PathAndQuery.Replace(".mvc", ""));
-
}
-
}
-
}
-
- Restart IIS just to be neurotic, like me.
- Build, publish, and load your application up in a web browser to give it a try.
Using these steps, I was able to get ASP.NET MVC URLs working on IIS without having the change my routes, which was ideal for me. When the upgrade for IIS7 comes hopefully I won't have to do anything too involved.
I hope these steps are helpful to those who want to try to the same thing. If I left anything out, please let me know. Best of luck!
Tags: .net, asp.net, csharp, iis, ionic, isapi, mvc, url
Related posts

Discussion
July 24th, 2008 at 11:07 pm
[...] to VoteASP.NET MVC URLs in IIS 6 using Ionic ISAPI Rewrite Filter (7/24/2008)Thursday, July 24, 2008 from http://www.brianlegros.comCreate a mapping for the “. mvc” extension in IIS to [...]
August 13th, 2008 at 12:25 am
ASP.NET MVC is introducing a number of brilliant enhancements that make .NET web development much neater than ever before. One of them is the new routing system, which makes it dead easy to handle “clean” URLs, even automatically generating outbound URLs from the same schema.
Add A Comment