If you are running IIS6 and want to deploy an ASP.NET MVC app it is a royal pain in the behind. Pretty URLs (www.mysite.com/prices instead of www.mysite.com/prices.aspx) 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
- Download Ionic Isapi Rewrite http://iirf.codeplex.com/
- Unzip the contents
- Create the following folder c:\inetpub\IonicRewriter (or a location of your choice)
- Copy IonicIsapiRewriter-2.0-Release-bin\bin\IIRF.dll (from the contents of the download you unzipped) to c:\inetpub\IonicRewrite
- Create a new text document, name it IirfGlobal.ini
- Select both files, right click and choose Properties
- Select the Security tab
- Ensure IIS_WPG has read & execute permissions on both IIRF.dll and IirfGlobal.ini
- 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. mysite.com/Content.mvc/site.css) To add another other folders you want to exclude, just add another |FolderName to the regular expression. - Copy the IirfGlobal.ini file to your website directory and rename it to IIRF.ini
- Select IIRF.ini, right click and choose Properties
- 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.
- Open IIS Manager
- Expand the Web Sites node
- Right click on the web site you want to configure and select Properties
- Select the ISAPI Filters tab
- Click the Add button
- Enter "Ionic Rewriter" for the filter name
- Browse to c:\inetpub\IonicRewriter\IIRF.ddl and select the file (or the location that you placed IIRF.dll into in Step 3)
- Click OK
- Select the Home Directory tab
- Click Configuration
- Click the Add... button under "Application extentions"
- Click the Browse button and select C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll for the executable
- Enter .mvc for the extension
- Limit verbs to: GET,HEAD,POST,DEBUG
- UNCHECK "Verify that file exists"
- Click OK
- Click OK, OK to close the main properties dialog
- In IIS Manager, go to Application Pools and Start/Stop the application pool that your MVC application is running under
- In IIS Manager –> Web Sites, select your MVC web site and stop/start the web site
IIRF Status and Troubleshooting
- On the server, open Internet Explorer and browse to: http://yourmvcwebsiteurl/iirfStatus
- You should see the IIRF Status Report if you followed the steps properly
- 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
- 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”
- 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
- Modify your Global.asax route code to handle .mvc routes as follows
routes.MapRoute(
"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.)
);
- Build & publisher your MVC application
- Copy your MVC application to your server, restart the application pool once more and your nice routes should now work
Download
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
http://www.churchofficeonline.com
2 comments:
Thank you very much. All the best.
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!
Post a Comment