Reader Level:

Web Config Transformation in .NET

By Amit Patel on Jul 24, 2012
In this article I am going to explain the Web.Config Transformtion feature of .NET 4.0.

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.. 

Amit Patel
Amit Patel

I am  C# Corner MVP in Q2 2012.  I have 10 years of experiences in Microsoft technologies. I am very much passionate about Web technology. My core skill is ASP.NET, WCF, C#, JQuery and CSS.



Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.

Trending up