Setting up a web project environment in Visual Studio 2010 to allow debugging using both IIS7 and the development Web server
We have run into an issue recently when importing a number of our legacy web projects into Visual Studio 2010. It seems that some projects don’t provide the options to allow us to run and debug them on the local version of IIS. For some reason, when a project is initially configured in VS2010 it sets a flag that determines whether the project properties page is shown inside the project window (where it will contain the necessary configuration options) or the project properties page is shown as a pop-up dialog window (where it doesn’t contain the options to allow debugging on a local IIS).
Here are the notes you will need when setting up a new project to guarantee that you have access to the required configuration page.
IIS 7 Configuration
The first step is entirely optional, as it is perfectly reasonable to run multiple Web Applications within a singe Web Site, however I prefer having separate Web Sites for different projects (or clients) as this allows me to control individual settings for each of them (for example host and port bindings) without me worrying about breaking a different project.
If you will use “localhost” to access the server, then you will only be able to have one Web Site instance running at a time, but this is rarely an issue, and you can easily run more than one site at a time by using a unique hostname or port for each.
Create the site in IIS Manager:
Where you create the Web Site mostly doesn’t matter, Visual Studio will add your project to the site as a virtual directory and the physical files can exist anywhere on your filesystem.
Once the site has been created, stop any other site that is using port 80, and start the Web site from the actions panel at the right of the window.
Because you want Visual Studio to modify the IIS configuration, you will need to run Visual Studio as Administrator.
Create an empty Web Application
Create a new project in Visual Studio (File > New > Project…).
Select: Visual C# > Web > ASP.NET Empty Web Application.
If you leave the “Create directory for solution” checkbox selected, then all your project files will end up one directory below the solution file. Personally I don’t mind having the .sln file in the same folder as my project files so I uncheck it.
If you click OK to create the various project files, then immediately compile and run the application you will see that by default Visual Studio has configured the development web server.
Reconfigure the project to use IIS
Now whilst this is useful for small standalone projects, we would ideally like to be able to switch development to the full version of IIS7, and to do this, we need to perform another couple of steps.
Back in Visual Studio, stop the project and in the Solution Explorer, right-click on the project and select “Properties”.
In the window that appears, select the Web option and scroll down until you can see the Servers section. Change the selection from “Use Visual Studio Development Server” to “Use Local IIS Web server”.
Click the “Create Virtual Directory” button to complete the IIS configuration. If you see the dialog below then everything has gone as expected and you can now run (and debug) your web application in IIS. You can switch between IIS and Cassini (the development web server) simply by changing the radio button back.
Using the same web.config for both IIS and Cassini
Because IIS7 uses a new integrated pipeline, and Cassini uses the classic pipeline, some of the settings in the web.config are not compatible. Whilst it is possible to use two web.config files (and move them in and out of the application directory when you switch webservers), it is much more convenient to use a single web.config with both webservers.
.NET will complain about the legacy settings if you are using the integrated pipeline (which we normally will want to do) unless you have the following included in the system.webServer section in the web.config:
<validation validateIntegratedModeConfiguration="false" />
This line instructs the .NET not to validate any settings it finds in the web.config for the integrated pipeline – this allows you to have settings that only work in classic mode (such as httpHandlers and httpModules co-existing with the handlers and modules sections that are required by IIS7.