Monday, January 25, 2010

Using Ionics Isapi Rewrite for ASP.NET MVC in IIS 6

If you are running IIS6 and want to deploy an ASP.NET MVC app it is a royal pain in the behind. Pretty URLs ( instead of aren’t supported out of the box. There are several solutions to this problem. First, you can still use MVC with IIS6 out of the box and just append .mvc to all of your files. This approach works, but you lose the nice URLs. Second, you can add a wildcard mapping that processes EVERY resource in a website through the ASP.NET ISAPI dll (images, css files, javascript, everything!) There is a known performance hit with this method and I found it to be quite noticeable when I tried it. Third, you can configure a URL re-writing module that will enable pretty URLs and do so in a well performing manner.

I wanted to go with option 3, which is a bit more work but I feel the best overall solution. Unfortunately, most of the information and “How To” posts I found out on the internet didn’t work, were horribly outdated or just didn’t make sense. So with that in mind I download the latest version of Ionic’s open source Isapi Rewrite module, read through their documentation and then worked out a configuration in IIS 6 that got everything working. Here is a step by step process that you can follow to easily (and freely) get ASP.NET MVC pretty URLs running under IIS 6 using Ionic’s Isapi Rewrite module.

Ionics Isapi Rewrite Installation and Configuration

  1. Download Ionic Isapi Rewrite
  2. Unzip the contents
  3. Create the following folder c:\inetpub\IonicRewriter (or a location of your choice)
  4. Copy IonicIsapiRewriter-2.0-Release-bin\bin\IIRF.dll (from the contents of the download you unzipped) to c:\inetpub\IonicRewrite
  5. Create a new text document, name it IirfGlobal.ini
  6. Select both files, right click and choose Properties
  7. Select the Security tab
  8. Ensure IIS_WPG has read & execute permissions on both IIRF.dll and IirfGlobal.ini
  9. Edit IirfGlobal.ini and include the following:
    # IsapiRewrite4.ini
    ## Turn off logging, enable if you need to debug routing
    #RewriteLog  c:\_Logs\iirfLog.out
    #RewriteLogLevel 3
    RewriteFilterPriority HIGH
    IterationLimit 1
    RewriteEngine ON
    StatusUrl /iirfStatus
    RewriteRule ^/Default\.aspx /Home.mvc [I,L]
    RewriteRule ^/$ /Home.mvc [I,L]
    RewriteRule ^/([\w]+)$ /$1.mvc [I,L]
    RewriteRule ^/(?!Content|Scripts|App_Data|Images)([\w]*)/(.*) /$1.mvc/$2 [I,L]

    ** The last rule filters out any files in the Content, Scripts, App_Data and Images folder from the URL rewrite. This prevents a .MVC extension from being appended to root folder of the resource being requested (i.e. To add another other folders you want to exclude, just add another |FolderName to the regular expression.

  10. Copy the IirfGlobal.ini file to your website directory and rename it to IIRF.ini
  11. Select IIRF.ini, right click and choose Properties
  12. Ensure IIS_WPG has read & execute permissions on IIRF.ini

Internet Information Services (IIS) 6 Configuration

** These configuration instructions are for a single website. If you want to configure the Ionics Isapi Rewrite module for all of IIS 6, perform these steps on the Websites root directory.

  1. Open IIS Manager
  2. Expand the Web Sites node
  3. Right click on the web site you want to configure and select Properties
  4. Select the ISAPI Filters tab
  5. Click the Add button
  6. Enter "Ionic Rewriter" for the filter name
  7. Browse to c:\inetpub\IonicRewriter\IIRF.ddl and select the file (or the location that you placed IIRF.dll into in Step 3)
  8. Click OK
  9. Select the Home Directory tab
  10. Click Configuration
  11. Click the Add... button under "Application extentions"
  12. Click the Browse button and select C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll for the executable
  13. Enter .mvc for the extension
  14. Limit verbs to: GET,HEAD,POST,DEBUG
  15. UNCHECK "Verify that file exists"
  16. Click OK
  17. Click OK, OK to close the main properties dialog
  18. In IIS Manager, go to Application Pools and Start/Stop the application pool that your MVC application is running under
  19. In IIS Manager –> Web Sites, select your MVC web site and stop/start the web site

IIRF Status and Troubleshooting

  1. On the server, open Internet Explorer and browse to: http://yourmvcwebsiteurl/iirfStatus
  2. You should see the IIRF Status Report if you followed the steps properly
  3. Specifically check the INI file status for both the Global and Site Specific sections, if there are any problems double check your file locations and file permissions as stated above
  4. If IIRF still isn’t working correctly, consult the IIRF v2.0 Operator’s Guide located in the contents of the zip file you downloaded under AdminGuide/Help. There is a section in the chm titled “Verifying and Troubleshooting Installation”
  5. You can also enable logging in the ini files for IIRF and take a look at the IIRF log output to diagnose problems and errors. If there isn’t a log being generated and un-commenting the logging options that means that IIRF isn’t routing properly.

ASP.NET MVC Application Configuration

  1. Modify your Global.asax route code to handle .mvc routes as follows
        "Default.mvc",                                          // IIS 6 Ionic Isapi Rewrite support
        "{controller}.mvc/{action}/{id}",                       // URL with parameters
        new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
        new { controller = @"[^\.]*" }                          // Don't look for a controller for non mvc files (ico, images, etc.)

  2. Build & publisher your MVC application
  3. Copy your MVC application to your server, restart the application pool once more and your nice routes should now work


Here is a zip file containing the IIRF.dll, IirfGlobal.ini and IIRF.ini files as well as a sample ASP.NET MVC application with the Global.asax routing setup. It is everything you need and you can just copy/paste these files into your web application using the instructions above.

Aaron Schnieder


Unknown said...

Thank you very much. All the best.

Unknown said...

suuup my man! was doing a search on google, and your blog came up in the front page! Wasn't quit what I was looking for, but, nonetheless, very informative! Hope all is well! peace!