I wanted to do this post to cover a topic a lot of people ask me about, and that's Web.Config transforms. And I wanted to provide a blog post to describe this for those who are new to the topic.
Web.config transforms are a basic building block of change management and DevOps in the sense that every application has configuration settings that need to be changed for each environment and the web.config is a common "dumping ground" for these settings.
Now I'm not saying this is not a good idea, and the web.config is fine. But honestly, I've seen too many shops that manage changes by saying "Don't replace the web.config" or maintain separate copies. This is a fine approach provided you aren't automating your deployments. If you are, then you really want to make your web.configs changes with the environment and limit access to production.
So how do we add transforms, many newer projects are pre-configured to transforms and you can tell this by looking for the following in your project.
In the image above, under the Web.config is the "Web.Debug.config" and "Web.Release.config". These are the transforms for the project, and depending on if you select "Debug" or "Release" determines which transform is applied to the web.config.
If you don't see the transforms, and you are running a Web Application, right-click on the web.config and click on "Add Config Transform".
Now an important part to understand is what a transform is, and what it is not.
- IT IS => a way of managing configuration changes
- IT IS NOT => a separate copy of the web.config
The idea is that the web.config transforms are each a delta file, they contain only the changes and use special attributes to identify and make changes.
If you open the "Web.Release.Config", you will see the following.
Notice that it does not have all the attributes of the web.config, instead here if you look at the comments, it describes putting in the values that you want to change. So to start with, let's put in a connection string, the most common application change.
So far, it looks like a pretty basic connection string entry (except for the token "
_ DefaultConnection _". But so far, the entry doesn't look any different. The key difference is the attributes we add after. These are the locator and the transform as shown below:
The key attributes are:
xdt:Locator: is leveraged to find the tag within the web.config
xdt:Transform: is the method of changing this tag, normally "
The first attribute being "
xdt:Locator", which is leveraged for it to find the tag. This is shown below:
string in here tells it to find a "
Match(name)" which translates to "
Match" by the "
The second attribute is "
xdt:Transform" which tells it to replace the entire tag:
After you've implemented this, when we publish in release mode, I get the following for my web.config:
Now the use of the token here is so that release management can replace these tokens. For more details, look at Total ALM.