Web.config Transformation

Web.config transformation

In Visual Studio, when you create a new MVC project or Web Forms Application, have you ever noticed that two config files apart from Web.config are added automatically?

These are Web.Debug.config and Web.Release.config.

Transform File

A transform file is an XML file that specifies how the Web.config file should be changed when it is deployed.

Purpose of These Files

The purpose of these files is to generate a new Web.config file depending upon the environment. It's extremely useful to store settings in these files, for example

debug = true //in development

debug= false //in production.

The advantage is that you don't need to change the web.config since it is generated by the Web.Config Transformation Engine. It takes a source file (your project's original web.config file) and a transform file (for example web.release.config) and produces an output file (web.config that is ready for the production environment).

XML-Document-Transform (XDT)

XDT is a simple and straight forward method of transforming the web.config during publishing/packaging. Transformation actions are specified using XML attributes defined in the XML-Document-Transform namespace, that is mapped to the xdt prefix.

There are xdt:Transform and xdt:Locator attributes that we use in the Web.Config file.

So, xdt:Locator locates element(s) and xdt:transform specifies the action over it.

Example

  1. <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">    
  2.     <connectionStrings>    
  3.         <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString=""/>    
  4.      </connectionStrings>    
  5. </configuration> 

Web.config

  1. <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">    
  2.     <connectionStrings>    
  3.         <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="DataSource=localhost\SQLExpress;Databasename=master;Integrated Security=true;"    
  4.         xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>    
  5.      </connectionStrings>    
  6. </configuration> 
Result
  1. <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">    
  2.      <connectionStrings>    
  3.              <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="DataSource=localhost\SQLExpress;Databasename=master;Integrated Security=true;" />    
  4.     </connectionStrings>    
  5. </configuration>
You can try the above example in Visual Studio or online at:

https://webconfigtransformationtester.apphb.com/

Use transformation at MSBuild.

You can select the configuration file to build your project accordingly, for example:

msbuild solution.sln /p:Configuration=Release

msbuild solution.sln /p:Configuration=Debug

How these files are associated with the Web.config

The transformation files depend on the Web.config. To preview that, open your solution and right-click your Web project by selecting Unload Project. Then, right-click and select Edit Web.csproj and look for web.debug.config. You'll find the following XML structure:
  1. <Content Include="Web.Debug.config">    
  2.     <DependentUpon>Web.config</DependentUpon>    
  3.      <SubType>Designer</SubType>    
  4. </Content>
I'll write another article on MSBuild commands and how to add Web.Config transformations at debug itself.

Please post your comments or feedback.


Similar Articles