Remap IIS Home Directory from Visual Studio
Many web developers are still using Windows XP which means they are limited to a single website instance under IIS. If you're like me you probably have a /dev folder with a child folder for each project website. When you switch focus to another project you typically fire up IIS Manager, open the properties sheet for your default website, switch to the home directory tab, type in the new path and then hit the Ok button. Sure it doesn't take too long but what if you could do all of that from a single click within Visual Studio?
The following is a short batch file that will accomplish just that. Once the batch file is added as an external tool switching websites is a click away.
@echo off
SET str=%1
SET str=%str:~1,-3%
cscript.exe "c:\Inetpub\AdminScripts\adsutil.vbs"
SET /W3SVC/1/ROOT/Path %str%
rmdir /s /q "%userprofile%\VSWebCache"
pause
- Save the batch file in a sensible location. I use C:\Program Files\tforster\utils\setIISPath.bat.
- Fire up Visual Studio (I'm using VS2008 but this should work for VS2005) and choose Tools | External Tools
- Click the Add button.
- Give the new entry a title. Mine says "Set IIS Home"
- In the Command: field enter the full path to the newly created batch file.
- Click the arrow to the right of the Arguments: field and choose $(ProjectDir)
- Click OK.
Open up a web project then choose Set IIS Home (or whatever you named your entry) from the Tools menu. Open your web browser and browse to http://localhost. You should now see your default page (assuming that you have created a default page, otherwise try http://localhost/your_special_page).
If this doesn't work for your consider the following assumptions. You may need to tweak things if your development environment differs from mine.
- My setup assumes the default http port of 80
- My web root source code is in the root of the Visual Studio project directory
- IIS admin scripts are installed to the default path c:\Inetpub\AdminScripts\adsutil.vbs.
- Open Regedit
- Navigate to HKEY_CLASSES_ROOT\Folder\shell\
- Right-click on shell and choose New Key
- Name the key "Set IIS Home" or whatever you want to see in your right-click menu
- Open the new key and double-click the (Default) String Value to edit it.
- Enter the full path to your batch file plus a space and "%L". Mine entry looks like C:\Program Files\tforster\utils\setIISPath.bat "%L"
- Click OK
- Click the Add button.
- Give the new service a title. I usually put the port I'm going to use in brackets so it's easy to remember what I'm using.
- In the Command: field enter C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0\WebDev.WebServer.EXE. Check your system if you've installed Visual Studio in a location other than the default.
- Enter /port:<your_preferred_port_number> /path:"<path_to_your_project>" /vpath:"/" in the Arguments: field.
- Click OK.
- Thumbnail Service (8001)
- iPhone Service (8002)
- xyz Service (800n)
Tip: Add The Same Behaviour As A Windows Explorer Right-Click
Sometimes it's more convenient to switch default home directories from within Windows Explorer. If you're comfortable editing registry entries then try this:
Now browse to your development folder, right click a child folder containing a web project and choose Set IIS Home
Tip: Run Multiple IIS Compatible Web Servers On Windows XP
Windows XP is a great client operating system and a perfect Windows development platform except that Microsoft chose to limit the number of websites to 1. This was presumably to stop people from trying to use XP as a production server platform and thus denying Microsoft potential Windows Server licensing revenue. Microsoft reversed this behavhiour with the release of Vista but that doesn't help those of us that are still bound to XP at the office. This becomes particularly problematic when you are working with web services and need to connect your debugger to the consumer and service applications.
Anyone familiar with Visual Studio is aware of the built-in development web server. Typically hitting F5 invokes the server on a seemingly random http port. You can use the development server in conjunction with an IIS hosted site to build a development web services infrastructure. But it's annoying having to repeatedly edit web.config files to account for the different ports, not to mention that F5 starts the site in a virtual directory which is almost always different than production.
Did you know that you can invoke the development server manually? And if you can invoke it manually you can configure an external tool to do so with a single click?
I have a number of webservices that I routinely need to run on my development box. Each one has a pre-assigned port number and is root mapped, rather than hanging off virtual child directory. My Tools menu contains entries such as:
I can easily start one or more of these services, each running in a separate instance of the development web server and each can be connected to with the Visual Studio debugger. To configure your first service open Tools | External Tools
If you choose your new service from the Tools menu you should see the usual web developer icon appear in your task tray. Depending on how you have implemented your project you should be able to navigate to http://localhost:<your_port_preferred_port_number> and see your default page or service endpoint.
To connect to the running instance with the Visual Studio debugger goto Tools | Attach to Process... (or Ctrl+Alt+P), make sure the Transport is set to Default, scroll through the list of available processes until you see WebDev.WebServer.EXE. Double-click it attach. Note, if you are running multiple servers this way each will show up with the WebDev.WebServer.EXE process name. Check the title column to determine which one want to connect to.