Working with multiple Web.Config files
When dealing with multiple development environments, it can often be quite tricky to keep track of the different environment specific settings that you may have in place. Fortunately, Visual Studio has a handy feature that allows you to have more than one Web.Config file for different environments.
I have previously blogged about Web.Config transformations in Visual Studio and how they give you the ability to override Web.config settings based on Solution configuration (Debug / Release). However, you might want to have a different Web.Config file for each environment - for example Web.Staging.Config or Web.Live.Config. The best thing about this feature is that it is easy to set up and you can be up and running in no time.
By the end of this tutorial, you should be able to easily create and set up a Web.Config file depending on your different environments. There are two ways to achieve this, either by using the solution configurations in Visual Studio or by using the pubxml files in your project.
Adding a Web.Config transform by using the solution configuration
In Visual Studio, navigate to the Solution Configuration drop down list and choose "Configuration Manager".
Next, look for the Active solution configuration drop down and select "New".
Give your configuration a name - I am going to call mine "Staging". I am also going to copy the settings from another transform.
Finally, right-click on Web.Config file and choose "Add Config Transform".
Once complete, you should notice your new Web.Config transform.
That's it! Next time you deploy simply choose the appropriate solution configuration and your settings will be applied.
Adding a Web.Config transform by using the pubxml file
You can also achieve the same result by using a publish profile file (.pubxml). Start by navigating to your web project in the Solution Explorer and choosing "Add Config Transform". This will add a new config transform for that publish profile only.
Once complete, you should notice that the appropriate Web.Config transform has been created.
This feature does however require you to have the VS2012 Update 2 or the Azure SDK 2.0 for VS2010.
When you deploy your next project, simply select the solution configuration that you need from the Solution Configuration drop down and the settings in your Web.Config file will be applied.
Comments
Paul V - 11/25/2013
Very useful - Thank you!
De-Angelo - 1/22/2015
This was very useful. Before I found this article every time I tried
to add a Transformation it was grayed out. This helped a lot.
Thanks
Dean H - 1/27/2015
Are the transforms applied when testing locally or only when deploying
code? I have different connection strings for each configuration
(dev,test,qa,prod) but the different connections don't seem to apply
when running IIS locally, regardless of the setting of the Configuration
dropdown.
Vick - 2/10/2015
This means, we have to build the code 5 times for 5 different environments?
Is there any option if I don't want to build/compile my source code multiple times?
Here is my use case - We compile/build and package our solution/source code as msi. Now this msi gets promoted to diff env.
Vick - 2/10/2015
So how do I configure in msi (command line) instruction to create a
environment specific config file at the time of installation, not the
compile time.
Thanks
Bob - 9/1/2015
Instead of build the application x times, build it ones and use a
deploy application to deploy to the environments. During deploy, the
transformations are ran.
For example with Octopus Deploy.
Jeff - 10/1/2015
Same question as Dean H: Transformations don't seem to kick in when
launching from Visual Studio with the Start (play) button. Is that by
design, and is there a workaround? I'd like to be able to do quick
switches during development (so I can hit different databases for
testing).
Add your comment