When you create a new web application in Visual Studio 2010, you will by default get a configuration file like this:
The web.config transformations allow you to adapt your configuration settings for different builds or environments. We have all worked with development, test and production environments where our settings differ from each other like:
- Database connection strings
- Application settings
- Debug mode
- Folders or shares
When working with different environments, you are bound to be working with different configuration settings. Web transformations allows us to transform our configuration file to the correct configuration file for the environment we want to publish to. By default we get a Debug and Release transformation file. We want to add a tranformation file for developent, test and production environment.
1. How to use web.config transformations to manage your different configurations
For demo purposes we will take a simple web.config file which looks like the following:
This is the web.config file which is containing our development settings. We will start by changing our configurations by the Configuration Manager:
You can edit the current configurations (Debug and Release) and you can create new Configurations:
I changed my configurations to Test and Production:
Make sure your projects are linked to the correct configuration:
Update the web.config transformations by using the Add Config Transforms on the Web.config file. This option will only be available if there is a missing transformation file for one of the possible configurations.
The web.config transformation files will be updated now to match our current configurations:
If we have a look at one of these transformations files, it will look like this by default template:
Let’s change our Web.Production.config transformation file to something like this:
Basically we specify we are going to change the connection string with name “DefaultConnection” to the correct connection string for Production. The xdt:Transform and xdt:Locator attributes are used to specify what to transform and how to transform it. For our appSettings we specify the BackOfficeEmail setting should have it’s value changed to email@example.com, which is the value for the production environment. We also specify that we want to insert a new application setting.
Set the configuration to Production and build your project:
When we publish our web application:
We publish it to our file system:
The published web.config looks as following:
The web.config has the correct connection string and app settings for Production. If we decide to build our solution on the Test configuration, our web.config will contain the transformations we specified in our Test transformation file.
In this case I worked as following:
- Web.config: Contained the configuration settings for my development
- Web.Development.config: Contained an empty transformation file since my development configurations were specified in the default web.config
- Web.Test.config: Contained the transformations for the test environment
- Web.Production.config: Contained the transformations for the production environment
Another way to work could be:
- Web.config: Contains the configuration settings that are equal for all environments
- Web.Development.config: Inserts all configuration settings for development environment
- Web.Test.config: Inserts all configuration settings for test environment
- Web.Production.config: Inserts all configuration settings for production environment
However it’s all up to preference and what suits you best.
2. Locator and Transform attributes to manage different transformations
The Transform and Locator attribute allows a few different options which you can use to manage different transformations. The Locator attribute is used to define what element the transformation should be applied to. The Transform attribute is used to specify what you want to do with the element in the transformation.
The Locator attribute allows the following options:
The Transform attribute allows the following options:
Examples on the Locator and Transform option: msdn
Web.config transformations are an easy tool to adjust our configuration settings according to the configuration we want to build to without much hassle. You define the configurations and for each configuration your specify your configuration transformation file. When you need a specific build or publish, you specify what configuration you want it to be on and you will immediately have to configuration file with the specified settings for the chosen environment.