This series of articles shows how to automate the process of changing the config file when deploying an App to different destination environments.
- Configuration Transformation (1): Web.Config --- this article
- Configuration Transformation (2): App.Config --- SlowCheetah
This article shows how to automate the process of changing the Web.config file when deploying an ASP.NET Web App to different destination environments.
Introduction
The content of this article:
- Transform File
- Web.Config Transformation
- Create Custom Web.Config Transformation
Transform File
A transform file is an XML file that specifies how the Web.config file should be changed when it is deployed. Transformation actions are specified by using XML attributes that are defined in the XML-Document-Transform namespace, which is mapped to the xdt prefix.
The XML-Document-Transform namespace defines two attributes:
- Locator: specifies the Web.config element or set of elements that you want to change in some way.
- Transform: specifies what you want to do to the elements that the Locator attribute finds.
The following example shows the contents of a transform file that changes a connection string and replaces the customErrors element:
<?xml version="1.0"?>
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MyDB"
connectionString="value for the deployed Web.config file"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
</system.web>
</configuration>
Web.Config Transformation
Most applications have settings in the Web.config file that must be different when the application is deployed. Automating the process of making these changes keeps you from having to do them manually every time you deploy, which would be tedious and error-prone.
There are two ways to automate the process of changing Web.config file settings:
- Web.config transformations and
- Web Deploy parameters --- not discussed in this article
A Web.config transformation file contains XML markup that specifies how to change the Web.config file when it is deployed. You can specify different changes for specific build configurations and for specific publish profiles.
The default build configurations are Debug and Release,

and you can create custom build configurations. A publish profile typically corresponds to a destination environment.
Create Custom Web.Config Transformation
Configuration Manager:
We use a configuration manager to create a custom Web.Config transform. We have several ways to open a configuration manager:
From Visual studio Build Tab.

Right Click solution.

From environment windows.

From CPU windows.

Add New Project Configuration













Join the conversation! Your thoughts help the community grow.