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".

"Visual Studio Configuration Manager"

Next, look for the Active solution configuration drop down and select "New".

"Visual Studio Configuration Manager New"

Give your configuration a name - I am going to call mine "Staging". I am also going to copy the settings from another transform.

"Visual Studio New Solution Configuration"

Finally, right-click on Web.Config file and choose "Add Config Transform".

"Visual Studio Add Config Transform"

Once complete, you should notice your new Web.Config transform.

"Visual Studio Web.Config"

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.

"Visual Studio pubxml"

Once complete, you should notice that the appropriate Web.Config transform has been created.

"Visual Studio pubxml"

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.