Multibranch support using Visual Studio and local IIS webserver
Do you know these projects where you as a developer have to work on multiple branches? And you have to switch to these branches during the day? Fix a bug here, add some new functionality in the other branch? I do….
What I found the most annoying where the projects that have besides a web project multiple web services inside the solution, and there all are using the local IIS web server to run on.
There is nothing wrong with running these web services on the local IIS, instead I prefer running them in IIS. What bothers me is that for every branch switching I’m getting an message stating:
The local IIS URL http://localhost/MultiBranchSupport/ specified for Web project MultiBranchSupport has not been configured. In order to open this project the virtual directory needs to be configured. Would you like to create the virtual directory now?
If I than say Yes, create a virtual directory for me, it gives an error message that the virtual directory already exists.
So how to fix this issue?
Well we could do some configuration changes to the web.config in every branch, so it points to a different location/virtual directory. That’s still too many manual changes, and would give on a merge action always trouble because the configuration files differ.
What I did was the following, create in IIS for every branch a new website. But before creating the new one, stop the existing one. By stopping the existing website, you have the ability to create a new website at the same port, in my case port 80. Point these website to a different location on you harddrive, for example D:TempMainBranch or the root folder of your branch.
Just before opening the solution of the new branch I stop the previous website and start the website for the branch I want to use. Than the solution isn’t even asking to create a new virtual directory. (Except for the first time.) In IIS Manager you will see the different websites, and the virtual directories you have for the different branches.

To make life even easier, I have created a few batch files which I can run to start the correct website and stop all other instances. To make this batch files, you have to use the iisweb command. This can be found in the Systemroot/System32 folder.
Run this command with administrator rights from the command prompt to give you an overview of the running websites in IIS:
iisweb /query
Site Name (Metabase Path) Status IP Port Host
Default Web Site (W3SVC/1) STOPPED ALL 90 N/A
MainBranch (W3SVC/1948497947) STARTED 127.0.0.1 80 N/A
DevBranch (W3SVC/1211348328) STOPPED 127.0.0.1 80 N/A
iisweb /stop MainBranch
iisweb /start DevBranch
Combining above commands into one batch file, will first stop the running MainBranch, and after that it will start the stopped DevBranch. Instead of using the name of the website after the stop/start command, you could also use the metabase path, to execute the command. The metabase path is also shown in the iisweb / query command, and looks like the following format W3SVC/1
In case you run this script twice, it will give an message that the websites are already stopped/running.
Probably there are even solutions where these batch files can somehow be linked to the solution, so that it will run before opening the projects within the solution, but I haven’t gone that far. If you have some other solution to support multiple branches on IIS, I would love to hear from you.
