Main Contents

Running ASP.NET MVC Applications Under IIS6

Schotime @ October 18, 2008

.NET | Comments (2)

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

#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


book mark Running ASP.NET MVC Applications Under IIS6 in del.icio.us submit Running ASP.NET MVC Applications Under IIS6 to digg.com

Dataset, Datatable to Json

Schotime @ July 27, 2008

.NET | Comments (4)

After my previous posts about returning data to the client as a JSON object, I decided to have a go at returning a generic Datatable/Dataset. This however is not as easy as simple returning a Datatable in your code behind method or web service. There is a solution though and here it is.
If you break [...]


book mark Dataset, Datatable to Json in del.icio.us submit Dataset, Datatable to Json to digg.com

jQuery Plugin for ASP.net Ajax (jMsAjax)

Schotime @ July 1, 2008

.NET | Comments (5)

After my recent post on jQuery, JSON and dates in asp.net I decided that there must be a better solution so I set out writing my first jQuery plugin. And after a few hours I had it working to my delight.
It not only accepts a raw JSON object as input for method parameters, but safely [...]


book mark jQuery Plugin for ASP.net Ajax (jMsAjax) in del.icio.us submit jQuery Plugin for ASP.net Ajax (jMsAjax) to digg.com

jQuery, AJAX, ASP.NET and Dates

Schotime @ June 19, 2008

.NET | Comments (3)

Just recently I discovered jQuery and I have to say….I’m a huge fan already. So I decided to setup my first AJAX call through jQuery and call a page method. After some playing around I finally had it. Thanks also go out to www.encosia.com. Thanks Dave. The only problem was that I could not parse [...]


book mark jQuery, AJAX, ASP.NET and Dates in del.icio.us submit jQuery, AJAX, ASP.NET and Dates to digg.com

Slow TcpClient Connection (sockets)

Schotime @ May 27, 2008

.NET | Comments (2)

Recently I have been experimenting with Sockets and trying to communicate with a windows service both with a console app and a website.
After managing to get some lines of communication going between the client and server applications, I couldn’t help but notice that it was taking a little bit longer than it probably should have. [...]


book mark Slow TcpClient Connection (sockets) in del.icio.us submit Slow TcpClient Connection (sockets) to digg.com

Binding A String to a Checkbox

Schotime @ April 2, 2008

.NET | Comments (2)

In a recent project I was attempting to list data from a configuration settings database table. The values were boolean but stored as a ‘Y’ or a ‘N’ so that only one set of SQL’s was needed to be written, as the product supports both Oracle and SQL Server.
When I first put the GridView [...]


book mark Binding A String to a Checkbox in del.icio.us submit Binding A String to a Checkbox to digg.com

Importing Data Files with Linq

Schotime @ March 18, 2008

.NET | Comments (5)

In my previous Linq post I discussed using Linq with Regular expressions and how much less code was needed. In this post we’ll again see how Linq can be used to speed up and simplify development.
There are many situations where you need to read data from a file into memory or a database. Lets consider [...]


book mark Importing Data Files with Linq in del.icio.us submit Importing Data Files with Linq to digg.com

Select All (ctrl+A) For A Textbox

Schotime @ March 12, 2008

.NET | Comments (4)

Have you ever tried to push ctrl+A on a multi-line or single line textbox in a Dot Net Windows Forms application to select all of the text? If you have you would know that it doesn’t work. That’s right, standard windows functionality doesn’t work for the TextBox control. So here’s how to fix it using [...]


book mark Select All (ctrl+A) For A Textbox in del.icio.us submit Select All (ctrl+A) For A Textbox to digg.com

Linq and Regular Expressions

Schotime @ March 10, 2008

.NET | Comments (4)

With Linq now standard in .NET 3.5, there is no reason why we shouldn’t use it. After all its full of features that can be used by any object that inherits the type IEnumberable. With such power at our fingertips, sorting, filtering, manipulation etc. etc. are available to us with fewer lines of code than [...]


book mark Linq and Regular Expressions in del.icio.us submit Linq and Regular Expressions to digg.com

Feed
661 spam comments
blocked by
Akismet