ASP.NET MVC on IIS 6 Walkthrough

I’ve seen a lot of reports where people have trouble getting ASP.NET MVC up and running on IIS 6. Sometimes the problem is a very minor misconfiguration, sometimes it’s a misunderstanding of how IIS 6 works.

In this post, I want to provide a definitive guide to getting ASP.NET MVC running on IIS 6. I will walk through using the .mvc or .aspx file extension for URLs first, then I will walkthrough using extension-less URLs.

If you’re running into problems with IIS 6 and ASP.NET MVC, I recommend trying to walk through all the steps in this post, even if you’re not interested in using the .mvc or .aspx mapping. Some of the lessons learned here have more to do with how ASP.NET itself works with IIS 6 than anything specific to ASP.NET MVC.

Initial Setup

To make this easy, start Visual Studio and create a new ASP.NET MVC Web Application Project on the machine with IIS 6. If your IIS 6 machine is on a different machine, you can skip this step. We can deploy the site to the machine later.

After you create the project, right click the project and select Properties. The project properties editor will open up. Select the Web tab and select Use IIS Web Server. Click on the image for a full size view.

Project Properties Editor

In the project URL, I gave it a virtual application name of Iis6DemoWeb and then checked Create Virtual Directory. A dialog box should appear and you should now have an IIS virtual application (note this is different than a virtual directory, as indicated by the gear looking icon) under your Default Web Site.

IIS 6 Virtual Web Application

Using a URL File Extensions

When you run the ASP.NET MVC installer, it will set up an ISAPI mapping in IIS 6 to map the .mvc extension to the aspnet_isapi.dll. This is necessary in order for IIS to hand off requests using the .mvc file extension to ASP.NET.

If you’re planning to use extension-less URLs, you can skip this section, but it may be useful to read anyways as it has some information you’ll need to know when setting up extension-less URLs.

Mapping .mvc to ASP.NET

If you plan to use the .mvc URL extension, and are going to deploy to IIS 6 on a machine that does not have ASP.NET MVC installed, you’ll need to create this mapping by performing the following steps.

One nice benefit of using the .aspx extension instead of .mvc is that you don’t have to worry about mapping the .aspx extension. It should already be mapped assuming you have ASP.NET installed properly on the machine.

For the rest of you, start by right clicking on the Virtual Application node (IIS6DemoWeb in this case) and select Properties. You should see the following dialog.

Website Properties

Make sure you’re on the Virtual Directory tab and select Configuration. Note that you can also choose to make this change on the root website, in which case the tab you’re looking for is Home Directory not Virtual Directory.

This will bring up the Application Configuration dialog which displays a list of ISAPI mappings. Scroll down to see if .mvc is in the list.

application mappings

In the screenshot, you can see that .mvc is in the list. If it is in the list on your machine, you can skip ahead to the next section. If it’s not in the list for you, let’s add it to the list. You’re going to need to know the path to the aspnet_isapi.dll first. On my machine, it is:

c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll

It might differ on your machine. One easy way to find out is to find the .aspx extension in the list and double click it to bring up the mapping dialog.

extension mapping

Now you can copy the path in the Executable text box to your clipboard. This is the path you’ll want to map .mvc to.

Click Cancel to go back to the Application Configuration dialog and then click Add which will bring up an empty Add/Edit Application Extension Mapping dialog.

Fill in the fields with the exact same values as you saw for .aspx, except the extension should be “.mvc” without the quotes. Click OK and you’re done with the mapping.

Specifying Routes with an Extension

Before we run the application, we need to update the default routes to look for the file extension we chose, whether it be .mvc or .aspx extension. Here is the RegisterRoutes method in my Global.asax.cs file using the .mvc extension. If you want to use the .aspx extension, just replace {controller}.mvc with {controller}.aspx.

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

  routes.MapRoute(
    "Default",
    "{controller}.mvc/{action}/{id}",
    new { action = "Index", id = "" }
  );

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

Note that because the second route, “Default”, has a literal extension as part of the URL segment, it cannot match a request for the application root. That’s why I have a third route named “Root” which can match requests for the application root.

Now, I can hit CTRL+F5 (or browse my website) and I should see the following home page.

Home Page

And about page.

About Page

Notice that the URLs contain the .mvc extension.

Uh oh, Houston! We have a problem

Of course, you’re going to want to be able to navigate to the web root for your project. Notice what happens when you navigate to /Iis6DemoWeb.

Root Home Page

This is a bug in the Default.aspx.cs file included with our default template which I discovered as I was writing this walkthrough. We’ll fix it right away, but I can provide the fix here as it’s insanely easy.

Note: If you received a File Not Found error when visiting the root, then you might not have Default.aspx mapped as a default document. Follow these steps to add Default.aspx as a default document.

As I’ve written before, this file is necessary for IIS 6, IIS 7 Classic Mode, and pre SP1 Cassini, but not IIS 7 Integrated. So if you’re using Cassini with Visual Studio 2008 SP1 and deploying to IIS 7 Integrated, you can delete Default.aspx and its sub-files.

In the meanwhile, the fix is to make the following change.

Change:

HttpContext.Current.RewritePath(Request.ApplicationPath);

To

HttpContext.Current.RewritePath(Request.ApplicationPath, false);

If you created your website in the IIS root rather than a virtual application, you would never have noticed this issue. But in the virtual application, the URL to the stylesheet rendered contained the virtual application name, when it shouldn’t. Changing the second argument to false fixes this.

IIS6 Extension-less URLs

Ok, now we’re ready to try this with extension-less URLs using the infamous “Star mapping” or “Wildcard mapping” feature of IIS 6. I say infamous because there is a lot of concern over the performance implications of doing this. Of course, you should measure the performance of your site for yourself to determine if it is really a problem.

The first step is to go back to the Application Configuration Properties dialog like we did when configuring the .mvc ISAPI mapping (see, I told you that information might come in useful later).

application mappings

Next to the Wildcard application maps section, click the Insert… button.

wildcard extension mapping

This brings up the wildcard application mapping dialog. Enter the path to the aspnet_isapi.dll. You can follow the trick we mentioned earlier for getting this path.

Don’t forget to uncheck the Verify that file exists checkbox! This is one of the most common mistakes people make.

If you’ve been following along everything in this post, you’ll need to go back and reset the routes in your Global.asax.cs file to the default routes. You no longer need the .mvc file extension in the routes. At this point, you can also remove Default.aspx if you’d like. It’s not needed.

Now when you browse your site, your URLs will not have a file extension as you can see in the following screenshots.

Home page without extension

About page without extension

 

Final Tips

One thing to understand is that an ASP.NET project is scoped to the Website or Virtual Application in which it resides. For example, in the example I have here, we pointed a Virtual Application named IIS6DemoWeb to the directory containing my ASP.NET MVC web application.

Thus, only requests for that virtual application will be handled by my web application. I cannot make a request for http://localhost/ in this case and expect it to be handled by my application. Nor can I expect routing in this application to handle requests for another root directory such as http://localhost/not-my-app/.

This might seem like an obvious thing to say, but I know it trips some people up. Also, in the example I did here, I used a virtual application for demonstration purposes. It’s very easy to point a root Website in IIS to my application and run it in http://localhost/ rather than a virtual application. This is not a problem. I hope you found this helpful.

Technorati Tags: ,,

What others have said

Requesting Gravatar... huey Nov 26, 2008 3:59 PM
# re: ASP.NET MVC on IIS 6 Walkthrough
Good to know the url problem when looking at the root request was a bug with Default.aspx.cs that is going to be fixed. I have been using Url.Content everywhere instead which can get ugly. For instance after the SP1 jquery patch I had two links to jquery -- one with url.content that didn't provide intellisense but would work when deployed, and another wrapped in a if(false) block just to provide intellisense.
Requesting Gravatar... xulijie Nov 26, 2008 4:32 PM
# re: ASP.NET MVC on IIS 6 Walkthrough
THANK YOU VERY MUCH,i CAN DO IT NOW
Requesting Gravatar... Martin Nov 27, 2008 12:29 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
thanks. this info is very helpful
Requesting Gravatar... Mike Nov 27, 2008 2:58 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
An alternative I like is to map .asp to ASP.NET MVC. If you don't use Classic ASP that extension should be available. It would look like this:

http://example.com/products.asp/detail/25
Requesting Gravatar... Brian Lowe Nov 27, 2008 4:42 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
It's all very well talking about mapping extensions to get IIS to handle the requests, but I expect lots of developers will be using shared hosting and have no access to the IIS mapping table. In this scenario the olny option is to make sure all of your routes include an extension that is already mapped to thee ASP.Net runtime under the default IIS set-up (.aspx is a sure thing but there are others).

Also, the default page configured under IIS (usually including default.aspx) must implement the necessary redirect code to get the initial route into the visitor's view...

public void Page_Load(object sender, System.EventArgs e)
{
HttpContext.Current.RewritePath(Request.ApplicationPath);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
}
Requesting Gravatar... Steve Nov 27, 2008 7:32 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Good post - thanks Phil!

Requesting Gravatar... Mitch Nov 27, 2008 7:43 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
One of the things that tripped me up at first when manually adding the .mvc extension was that I was not leaving the verify the files exists checkbox unchecked. It seems obvious, but for some reason it made waste some time running the problem down.

Thanks,
Mitch
Requesting Gravatar... alik levin Nov 29, 2008 9:41 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
great post!
thanks for sharing it, very helpful to bootstrap with MVC/IIS.
Requesting Gravatar... Shaun Trennery Dec 01, 2008 7:34 PM
# re: ASP.NET MVC on IIS 6 Walkthrough
There is a problem with AntiForgeryToken in MVC Futures and running under a virtual directory in IIS6. I get the following error:

"The virtual path '/DummyUrl' maps to another application, which is not allowed"


Requesting Gravatar... prabhjot singh Dec 03, 2008 12:29 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Sir, i have used this method, but i am facing scalability problem, i want to use the 404 approach of extension less routing. i have implemented everything i.e. i have written the required code for 404 in the global file and changed the IIS6 settings at the time of deployment. but it is still throwing the " Resource cannot be found" error.

what should i do??

Regards
Prabhjot Singh
Requesting Gravatar... Gordon Bergling Dec 03, 2008 12:48 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Thank you for that Guide...
Requesting Gravatar... Luke Venediger Dec 03, 2008 1:00 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Thanks Phil!!! Yay! No more .mvc extensions in my routes.
Requesting Gravatar... Brien Dec 03, 2008 5:44 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Don't forget that in a basic IIS install, ASP.NET is turned off. You need to go into the IIS Web Service Extensions and enable ASP.NET.

I fought with IIS for a few days before I realized my mistake.
Requesting Gravatar... venu Dec 04, 2008 3:18 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
hi,

How to configure IIS 6.0 + HTTPS + Mvc Preview 3

reply as soon as possible

thanking you
Requesting Gravatar... haacked Dec 04, 2008 9:26 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
@venu sorry, but we don't support ASP.NET MVC Preview 3. I suggest you upgrade to the Beta.
Requesting Gravatar... Venu Dec 04, 2008 11:05 PM
# ASP.NET MVC on IIS 6 Walkthrough

hi haacked,

Thanks for the Reply.

Now i'm using MVC Beta and i publish the application like
http://localhost/MVCTest

If i run the application from the solution then i can access all pages like Home ,About and

Login Page.

But when i access the link [http://localhost/MVCTest] from IIS or IE, i can access only Home

page.If I'm trying to access About page or Login page it shows "The page cannot be found".

What can i do to access the About and Login page from IIS or IE ?

Reply Immediately.

by,
Venu
Requesting Gravatar... Michael Knopf Dec 08, 2008 6:48 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Thanks for the info, I am familiar with using the wildcard mapping as this was necessary in an app i wrote that used URL re-writing but I find the IIS Virtual "Application" as opposed to directory interesting. Thanks again Haack, your posts are invaluable
Requesting Gravatar... Mats Jakobsson Dec 10, 2008 6:26 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Beautiful! You really made my day with this walkthrough! Thanks a lot!
Requesting Gravatar... Eric Polerecky Dec 10, 2008 7:09 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
I do not have wildcard mappings available on this version of XP.

First, is there a service pack or otherwise update to IIS for XP?

Second, I added a * mapping under application mappings...with worked to remove the .mvc extension...but breaks my web services

The HTTP verb POST used to access path 'Web site name' is not allowed


Any hints?
Requesting Gravatar... Tauqeer Dec 10, 2008 10:47 PM
# re: ASP.NET MVC on IIS 6 Walkthrough
Thanks for the great post
Requesting Gravatar... Rey Dec 15, 2008 1:33 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Hi ,

I just deployed the sample MVC project into my website http://classifieds.arabeastern.com/ , The home page is working fine. All the links other than home page throw a page not found error. I had added the .mvc to the application configuration and unchecked "Verify the file exist" option.

Anyone please help me to get rid of this error

Home page url : http://classifieds.arabeastern.com/

Thank you.


Requesting Gravatar... Hasan Ozdil Dec 19, 2008 4:33 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
I have the same problem than Rey.

Thx for any help.

Hasan
Requesting Gravatar... Dan Jan 05, 2009 7:23 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Hi,

Great walk through! I have one issue though. All is currently working well on my localhost server but when I attempt to view the page from another computer on my network, I receive the connect to computer pop up asking me to login as a guest and if I hit ok or cancel the webpage looks as if no style sheet has been applied (just like in the Uh oh, Houston! We have a problem screenshot)

Do you have any ideas how to remove this authorisation popup and allow the site to display as intended?

Kind regards,

Dan
Requesting Gravatar... OscarD Jan 08, 2009 1:21 PM
# re: ASP.NET MVC on IIS 6 Walkthrough
Rey, Hasan Ozdil :

You just forgot to do this step "IIS6 Extension-less URLs"

Adding the infamous “Star mapping” or “Wildcard mapping”
for Extension-less URLs

Requesting Gravatar... MUQSIT MUJAWAR Jan 28, 2009 3:44 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
It's all very well talking about mapping extensions to get IIS to handle the requests, but I expect lots of developers will be using shared hosting and have no access to the IIS mapping table. In this scenario the olny option is to make sure all of your routes include an extension that is already mapped to thee ASP.Net runtime under the default IIS set-up (.aspx is a sure thing but there are others).

Also, the default page configured under IIS (usually including default.aspx) must implement the necessary redirect code to get the initial route into the visitor's view...

public void Page_Load(object sender, System.EventArgs e)
{
HttpContext.Current.RewritePath(Request.ApplicationPath);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
}


MUQSIT MUJAWAR
Requesting Gravatar... Venu Jan 28, 2009 5:12 AM
# ASP.NET MVC on IIS 7
hi,

How to Configure Asp.net MVC Beta + Windows server 2008 + IIS 7.0 + PUT,DELETE Verb.


Requesting Gravatar... James Fisk Feb 02, 2009 11:59 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Thank you very much. I just upgraded to MVC RC and it just stopped working. This blog saved me a lot of hassle. It's all good now.
Requesting Gravatar... toddo Feb 06, 2009 2:54 PM
# re: ASP.NET MVC on IIS 6 Walkthrough
Hi, Trying to use file extension method with forms authentication and have authorization set to deny users="?" and I am getting "The page cannot be found" when I try to browse it and the URL changes to:

localhost/.../Login

I changed my RegisterRoutes() like below and have Default.aspx listed as a default document. What could I be missing?

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(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
Requesting Gravatar... Vasanth Mar 24, 2009 1:18 AM
# re: .aspx is not removed in URL
Hi,
I have followed this article to remove the extension. I have added .aspx insteadof .mvc in my routing. I am not able to remove the .aspx from the url.
I am using IIS6.0. But I dont know what may be the probs....
pls give me the solution....
Requesting Gravatar... Carl Mar 26, 2009 7:01 PM
# re: ASP.NET MVC on IIS 6 Walkthrough
thank you SO much for sharing this article, Phil! My MVC application works fine with my host and I am excited and enthusiastic to wait for your next post.

Keep up the good work, mate!!
Requesting Gravatar... Wayne Mar 26, 2009 7:06 PM
# re: ASP.NET MVC on IIS 6 Walkthrough
Everything works perfectly!!! I have tried my MVC site on both IIS6 and IIS7 with my current host (http://www.asphostcentral.com) and they are all working fine. I am really grateful with this well-written article and I certainly hope it will help the other community members in getting up their first MVC site.

Believe me, this technology is just amazing!!
Requesting Gravatar... Gopinath May 15, 2009 12:32 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Is there any way to get rid of infamous “Star mapping” or “Wildcard mapping” feature of IIS 6? I used the Wildcard mapping to resolve 404 errors.

I'm very much concerned about the performance.
-------------------------------------------------------
By the way, where is the subscription option to comments notification?
Requesting Gravatar... mr_fleisch Jun 04, 2009 3:59 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
thanks a lot, gread help! :-)
Requesting Gravatar... Anders Juul Jun 08, 2009 1:23 AM
# re: ASP.NET MVC on IIS 6 Walkthrough
Hi Phil,

Just what I needed to get up and running - saved me the trouble of upgrading just for the sake of mvc, so I can delay that for another time.

Thanks,

Anders, DK
Requesting Gravatar... Helen Jun 26, 2009 4:30 PM
# re: ASP.NET MVC on IIS 6 Walkthrough
Everything works fine I've tested on Window 2008 server of current host (www.Webhost4life.Org) and all working perfect!

MVC is crazy!
Requesting Gravatar... MKV to Divx converter Jun 29, 2009 7:30 PM
# re: ASP.NET MVC on IIS 6 Walkthrough
wow -Great post and awesome joke! ;)

What do you have to say?

(will show your gravatar)
Please add 6 and 2 and type the answer here: