ArticleDirectory
- Mapping. MVC to ASP. NET
- Uh oh, Houston! We have a problem
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. MVCOr. AspxFile 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. MVCOr. AspxMapping. 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 newASP. net mvc Web Application ProjectOn 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 selectProperties. The project properties editor will open up. SelectWebTab and selectUse IIS Web server.Click on the image for a full size view.
In the project URL, I gave it a virtual Application nameIis6demowebAnd then checkedCreate virtual directory. A dialog box shocould appear and you shocould 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.
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. MVCExtension toAspnet_isapi.dll. This is necessary in order for IIS to hand off requests using. MVCFile 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. MVCURL 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 executing the following steps.
One nice benefit of using. AspxExtension instead. MVCIs that you don't have to worry about mapping. AspxExtension. It shocould already be mapped assuming you have ASP. NET installed properly on the machine.
For the rest of you, start by Right clicking onVirtual ApplicationNode (Iis6demowebIn this case) and selectProperties. You shoshould see the following diwing.
Make sure you're onVirtual directoryTab and selectConfiguration. Note that you can also choose to make this change on the Root website, in which case the tab you're looking for isHome DirectoryNotVirtual directory.
This will bring upApplication configurationDialog which displays a list of ISAPI mappings. scroll down to see if. MVC is in the list.
In the screenshot, you can see that. MVCIs 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.
Now you can copy the path inExecutableText Box to your clipboard. This is the path you'll want to map. MVC.
Click Cancel to go back toApplication configurationDialog and then clickADd Which will bring up an emptyAdd/edit application extension mapping Dialog.
Fill in the fields with the exact same values as you saw for. aspx, could t the extension shocould be". MVC"Without the quotes. ClickOKAnd 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. MVCOr. AspxExtension. Here isRegisterroutes
Method in myGlobal. asax. CSFile Using. MVCExtension. If you want to use. AspxExtension, 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 shocould see the following home page.
And about page.
Notice that the URLs contain. MVCExtension.
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/Iis6demoweb.
This is a bug inDefault. aspx. CSFile was ded 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 have Ed 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 deleteDefault. aspxAnd 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 wowould 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 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 shocould measure the performance of your site for yourself to determine if it is really a problem.
The first step is to go back toApplication configuration PropertiesDialog like we did when locking ing. MVCISAPI mapping (See, I told you that information might come in useful later).
Next toWildcard application mapsSection, clickInsert...Button.
This brings up the wildcard application mapping dialog. Enter the path toAspnet_isapi.dll. You can follow the trick we mentioned earlier for getting this path.
Don't forget to uncheckVerify that file existsCheckbox!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 yourGlobal. asax. CSFile to the default routes. You no longer need. MVCFile Extension in the routes. At this point, you can also removeDefault. aspxIf 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.
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 namedIis6demowebTo 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 requestHttp: // localhost/In this case and perform CT it to be handled by my application. Nor can I perform CT routing in this application to handle requests for another root directory suchHttp: // 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.
From: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx