Monthly Archives: October 2008
Running ASP.NET MVC Applications Under IIS6
Yes…Finally…I have full control of my HTML again, but am still able to work with a great language like C#. MVC is here and its great. I’m absolutely loving it. But how the heck do you get it to work under IIS6. What if I want extensionless URL’s? Here is a few options for you.
Option 1:
Running IIS with Wildcard Application mapping.
This option allows extensionless URL’s but a what performance price. Every single request including images, files and css etc. will get passed through the aspnet_isapi.dll. For small sites this may not be a huge issue, but small sites soon because large sites and this then becomes an issue.
I’m not going to run through how to do this, because there are already some nice blogs about this and I don’t really recommend this option.
Option 2:
Running IIS with .mvc (or whatever you like) extensions.
I started out running my site (schotime.net) like this, however in the end I decided on Option 3.
URLs look like this. /Home.mvc/Index rather than /Home/Index which is not that bad but again not as nice as the latter.
Anyways here’s how you set this up.
Firstly your global.asax.cs should look like this.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MVCApplication1 { public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", "{controller}.mvc/{action}/{id}", new { controller = "Home", action = "Index", id = "" } ); routes.MapRoute( "Defaultest", "Default.aspx", new { controller = "Home", action = "Index", id = "" } ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); RouteTable.Routes.RouteExistingFiles = true; } } } |
This will get you started. All you need to do then is map the extension .mvc to the aspnet_isapi.dll in IIS under Home Directory -> Configuration -> Mappings or use an already mapped extension like .aspx and your mvc application should work.
Option 3:
This option requires the use of a clever isapi filter to rewrite the URLs.
I originally read an article on Bia Securities which uses the Isapi_rewrite 3rd party plugin however, Isapi_rewrite is not free so I thought I would find a suitable open source solution. Here it is: Ionic Rewriter -> http://www.codeplex.com/IIRF
It works in much the same way as the isapi_rewrite but will a few little differences. Once you download the IsapiRewrite4.dll, place the dll in a directory eg. C:\WINDOWS\system32\inetsrv\IIRF and run through the installation instructions included in the download (readme.txt) under Installation. Next comes the configuration. Firstly here is the IsapiRewrite4.ini configuration file ported from the Bia Securities article.
|
#RewriteLog c:\temp\iirfLog.out RewriteRule ^/Default\.aspx /Home.mvc [I,L] RewriteRule ^/$ /Home.mvc [I,L] RewriteRule ^/([\w]+)$ /$1.mvc [I,L] RewriteRule ^/(?!Content)([\w]*)/(.*) /$1.mvc/$2 [I,L] |
The [I,L] stands for a case-insensitive match and to stop processing if the current rule is a match.
Important Note:
Any physical files that you directly linked to should not be routed. In my MVC folder system I store all of these files under the /Content directory. Wherever you store your files replace the text Content with the folder that your files reside. otherwise images, css etc. will fail to display.
Now all that’s left to do is to modify the global.asax.cs to support this.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MVCApplication1 { public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" }, // Parameter defaults new { controller = @"[^\.]*" } ); routes.MapRoute( "Defaultmvc", // Route name "{controller}.mvc/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" }, // Parameter defaults new { controller = @"[^\.]*" } ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } } } |
Note: The line with this on it -> new { controller = @"[^\.]*" } is very important. The routes won’t work without it.
And that’s it! You’re done.
Personally I like the last option but if you have no access to your hosting web server then Option 2 is probably the best.
Schotime