Web Config Transformation in .NET

In this article I am going to explain a new feature of .NET 4.0. It is Web.Config Transformtion. Just imagine that you have multiple environments that you deploy your application to. So you must keep separate config files for each environment.

Let's see how we can do that using web config transformation.

Step 1: We will take a new web application project and we will see that we have two default web.config files in the Root itself.


Now just assume that we have a total of four environments:

  • Debug
  • DEV

Now we will add a separate web.config file for each environment.

Step 2: Now we will open the Configuration Manager in the Build Menu:


Step 3: In the Configuration Manager:


We will add a new configuration file:


We will do it for each environment and for each file we will right-click on web.config file and Add Config Transforms.


Now we have added four web.config files.

Step 4Now we open the web.Release.config file and we will make the same changes. We will only change the Connectionstring:

      <add name="MyDB"
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

Step 5: Now we will pubish our application.


And we will open the publish folder web.config file:

    <add name="MyDB"
      connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
      providerName="System.Data.SqlClient" />
        <compilation targetFramework="4.0" />

See, in the above web.config file now our Connection String is pointing to the Release Database.

There are many things we can do using:

xdt:Transform="SetAttributes" xdt:Locator="Match(name)"


Happy Coding..