When deploying a site from development to production, settings need to be changed in the web.config to adjust for things like connection strings, debug settings, or any other setting. While generally this is something easy to do changing these may be over looked, forgotten or someone may just miss changing a setting. This is where web.config transform really helps out and makes changing multiple settings happen with just changing a drop down list.
Creating a Transform File
1.) Open the Configuration Manager
Locate and open the ‘Configuration Manager’ located under the ‘Build’ menu.
2.) New Build Configuration
In the ‘Configuration Manager’, open the ‘Active solution configuration’ drop down and select a new build configuration.
3.) Give it a Name
Give the new configuration a name for what it will be used for (Dev, Production, Staging). This can be anything you want, but should be named something describing what it is for. I always copy the settings from ‘Release’ just because that is what is normally used to publish/package the site. Then click ‘OK’ and now you should see your new configuration show up as a build configuration which can be used in the same way you would use ‘debug’ or ‘release’.
4.) Find Your File
Next, find your Web.config file in the solution explorer. Right-click it and press ‘Add Config Transform’. This will create new config files for each build configuration created. You should see the new files as children of the Web.config. They will be named Web.<build name>.config
5.) Open the Build
Open up the config file for the build you wish to do work in. This file will have default comments that give examples of things that you can change. You can ignore or remove the comments if you want. They should not be pushed into the transform.
When adding/changing settings in this config file, make sure to build the tree the same way it is built in the normal web.config file. Below is an example that will look for a connection string that has the same name attribute and then change all the attributes to match this.
7.) Save It
After you finish making your changes to this config file, Save it. To view what the web.config will look like when running with the specified build configuration right click the file in the solution explorer and click on Preview Transform. This will bring up 2 web.configs, the original and the transformed. This allows you to compare them to see if you might have missed a setting.
You Did It!
Once complete, you will have transforms that can now be used to easily switch between setting during development and deployments. Using these transforms saves time and frustration because it helps prevent forgetting to switch a connection string or setting and cause you to be hitting the wrong environment at the wrong time. Or even worse, make a site not deploy properly because of missing values.
References: Microsoft MSDN
For more information about how to write transform files, see Walkthrough: Deploying a Web Application Project Using a Web Deployment Package (Part 1 of 4)
and Web.config Transformation Syntax for Web Application Project Deployment.