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:WINDOWSsystem32inetsrvIIRF 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:tempiirfLog.out

#RewriteLogLevel 3

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

Advertisements
This entry was posted in .NET and tagged , , . Bookmark the permalink.

8 Responses to Running ASP.NET MVC Applications Under IIS6

  1. Pingback: Running ASP.NET MVC Applications Under IIS6 - Adam Schroder

  2. Pingback: ASP.NET MVC Archived Blog Posts, Page 1

  3. dez says:

    I tried option 2 and a test.asp file loads fine but I cannot get any of my other MVC files to load, just 404 errors. So, then I tried option 1 and now I cannot even get the test.asp file to load.

    I’m running ASP.NET MVC Beta and pages load fine in my local web server inside VS2008. I’m just trying to get the default page and Home/About to load. I’m deploying files using ftp, deleting all targets before publishing from VS 2008 SP1. Here is my Global.asax file using option 1:

    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
    );
    }

    protected void Application_Start()
    {
    RegisterRoutes(RouteTable.Routes);
    }
    }

    Here is what I used in my Global.asax file for Option 2:

    public class MvcApplication : System.Web.HttpApplication
    {
    public static void RegisterRoutes(RouteCollection routes)
    {
    routes.IgnoreRoute(”{resource}.axd/{*pathInfo}”);

    routes.MapRoute(
    “Default”, // Route name
    “{controller}.aspx/{action}/{id}”, // URL with parameters
    new { controller = “Home”, action = “Index”, id = “” } // Parameter defaults
    );
    routes.MapRoute(
    “DefaultTest”, // Route name
    “Default.aspx”, // URL with parameters
    new { controller = “Home”, action = “Index”, id = “” } // Parameter defaults
    );

    }

    protected void Application_Start()
    {
    RegisterRoutes(RouteTable.Routes);
    }
    }

    I’m have my site set up as a virtual directory under the Default web site. Should The Documents-Enable Default Content Page be set for ASP.NET MVC apps? If so what should the single file entry be if you want it to load the default page (Default, Default.aspx, /Home/Index)?

  4. Schotime says:

    @dez:

    Just from looking quickly at your two attempts there looks like one distinct errors that may be causing the issue for option2.

    Option2:

    RouteTable.Routes.RouteExistingFiles = true;

    That needs to be included as above.

    As for option 1 not working i’m not really sure. As long as you have set the aspnet_isapi.dll up as the default for all request then it should work.

    The Default.aspx file should be fine as the default document, as this should still exist in your website.

  5. Jeff says:

    I am also trying option 2, and I am getting 404 errors when I try to run the site using IIS 6 or the web service project. Here’s my RegisterRoutes method:


    public static void RegisterRoutes(RouteCollection routes)
    {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
    "Default", // Route name
    "{controller}.aspx/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = "" } // Parameter defaults
    );

    routes.MapRoute(
    "Defaultest",
    "Default.aspx",
    new { controller = "Home", action = "Index", id = "" }
    );

    RouteTable.Routes.RouteExistingFiles = true;
    }

    Any ideas what could be wrong?

  6. Jeff says:

    Aha! I figured out Option 2. The second path needs to still be the old mapping, or the base address and Default.aspx won’t work right. Here’s the corrected code:


    public static void RegisterRoutes(RouteCollection routes)
    {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
    "Default", // Route name
    "{controller}.aspx/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = "" } // Parameter defaults
    );

    routes.MapRoute(
    "Default2", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = "" } // Parameter defaults
    );
    }

    The first one, Default, sets up the links so that they switch to having the .aspx extension (like Home.aspx). The second setting allows the root site with no page and also Default.aspx to be remapped properly to the Home controller.

  7. Gotta tell ya, I was extremly excited when I saw this post. One comment though, as it did cause a little frustration. I do have access to my own box, and wanted to do as little as possible to my MVC framework, so I went with option 3.

    I did however miss the step (only talked about in option 2) about adding .mvc to the aspnet MIME types. Very important everyone. It will save you some headaches, just wanted to make sure nobody else missed it.

    THANK YOU very much! This got me the last 1/4 mile in my MVC experimentation.

  8. You forgot one more option: Just use normal .aspx extension for MVC pages and you don’t need any configuration of the IIS at all.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s