Transform Web.Config File While Deploying The Web Application Project

One of the nice features that supports Visual Studio 2010 and above versions is Web.Config (XDT) transformations. Web.Config transformation is a great way to manage configurations in various environments. By using this feature, we can change the configuration settings (like app settings, DB setting, mail setting and others) very easily within our Web.config file when a web application or a website.

We know that when we deploy a web application or a website, we often want to change a few settings in the deployed web application's or website's Web.config file based on the host environment selection. For instance, we might want to change the app settings, connection strings, mail settings, proxy settings, active directory, security settings, service endpoints and network addresses and so on. It's not only changing the configuration settings but also inserting new settings, removing existing settings and so on during the deployment.

The transformation file contains XML markup code that tells how to change the Web.config file when it is deployed. Make a note- it works only for web applications or websites.

Follow the below steps to achieve the same:

Step #1 - Create a web application project or a website

Firstly, we have to create a web application or a website.

Step #2 - Right click on Web.config

Once the Web application or Website gets created, right-click on this and select an option "Add Config Transforms".

If you see the below screenshot, the "Add Config Transform" option is disabled by default. We have to do some configurations to make it enabled.

Transform Web.Config

Follow the below steps to enable the "Add Config Transform" option.

  • Refer the below screenshot and click on Configuration Manager from the list.

    Transform Web.Config
  • Once you click on Configuration Manager, the below screenshot will pop up. Select <New…>.

    Transform Web.Config
  • If you click on <New…> option, the below screenshot will be opened.

    Transform Web.Config

Give any name (for instance “UAT”) on which it is going to create a new Web.config transformation file. Select a "Copy settings from" option.

Don’t forget to select below options.

    • "Copy settings from" option because based on the selected option only, it is going to pull the settings.
    • "Create new project configurations" option checked; if you miss out this option, then "Add Config Transform" option is not going to enable.
  • Once you click on Ok, the child window will be closed and a new configuration will be visible as below.

    Transform Web.Config
  • That’s it. Now, click on Close button
  • Now, if you right-click on Web application or Website, you will see "Add Config Transforms" option enabled.

    Transform Web.Config

Step #3

If you click on Add Config Transforms - it will create the default web.debug.config and web.Release.config files for you and along with that the newly configured config file, that is, with name UAT.

Transform Web.Config

Step #4

Please make sure the root element of a transform file, that is, <configuration> must specify the XML-Document-Transform namespace. We can configure the required setting as per the business need. Here, I used a few features of ML-Document-Transform that I needed.

Web.Release.Config

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->  
  4. <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">  
  5.   <appsettings>  
  6.     <add key="email" value="xyz_Release@yahoo.com"  
  7.        xdt:Locator="Match(key)" xdt:Transform="RemoveAll" />  
  8.     <add key="eMail_From" value="xyz_Release@hotmail.com" xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)"/>  
  9.     <add key="eMail_CC" value="xyz_Release@yahoo.com" xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)"/>  
  10.     <add key="eMail_BCC" value="xyz_Release@gmail.com" xdt:Transform="Insert"/>  
  11.   </appsettings>  
  12.   <connectionstrings xdt:transform="Replace">  
  13.     <add name="Release_Mode" connectionstring="data source=My-PC;initial catalog=MyDB;   
  14.       user id=sa;password=test123" providername="System.Data.SqlClient"/>  
  15.   </connectionstrings>  
  16.   <system.web>  
  17.     <compilation xdt:Transform="RemoveAttributes(debug)" />  
  18.     <authentication mode="Windows" xdt:Transform="SetAttributes(mode)"/>  
  19.   </system.web>  
  20. </configuration> 

Web.Debug.Config

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->  
  4.   
  5. <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">  
  6.   <appsettings>  
  7.       <add key="email" value="xyz_Debug@yahoo.com"  
  8.          xdt:Locator="Match(key)" xdt:Transform="RemoveAll" />  
  9.       <add key="eMail_From" value="xyz_Debug@hotmail.com" xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)"/>  
  10.       <add key="eMail_CC" value="xyz_Debug@yahoo.com" xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)"/>  
  11.       <add key="eMail_BCC" value="xyz_Debug@gmail.com" xdt:Transform="Insert"/>  
  12.     </appsettings>  
  13.   <connectionstrings xdt:transform="Replace">  
  14.     <add name="Debug_Mode" connectionstring="data source=My-PC;initial catalog=MyDB;   
  15.       user id=sa;password=test123" providername="System.Data.SqlClient"/>  
  16.   </connectionstrings>  
  17.   <system.web>  
  18.     <compilation xdt:Transform="RemoveAttributes(debug)"/>  
  19.     <compilation batch="false" xdt:Transform="SetAttributes(batch)"/>  
  20.     <authentication mode="Windows" xdt:Transform="SetAttributes(mode)"/>  
  21.   </system.web>  
  22. </configuration>  

You can remove the existing values for the required settings and insert with new settings at the time of deployment by using xdt:Locator="Match(key)" xdt:Transform="RemoveAll" which removes the existing values, and then xdt:Transform="Insert" which will insert with new values. And also, xdt:Transform="Replace" will replace any existing elements. In addition, we can use RemoveAttributes and SetAttributes to remove existing attributes or to add new attributes.

Default Web.Config file

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3.   For more information on how to configure your ASP.NET application, please visit  
  4.   http://go.microsoft.com/fwlink/?LinkId=169433  
  5.   -->  
  6. <configuration>  
  7.   <appsettings>  
  8.     <add key="email" value="a11@yahoo.com" />  
  9.     <add key="eMail_From" value="b11@hotmail.com"/>  
  10.     <add key="eMail_CC" value="c11@yahoo.com"/>  
  11.   </appsettings>  
  12.   <connectionstrings>  
  13.     <add name="Config_Mode" connectionstring="data source=My-PC;initial catalog=MyDB;   
  14.       user id=sa;password=test123" providername="System.Data.SqlClient"/>  
  15.   </connectionstrings>  
  16.   <system.web>  
  17.     <compilation debug="true" targetFramework="4.5" />  
  18.     <httpRuntime targetFramework="4.5" />  
  19.   </system.web>  
  20. </configuration>  

Preview Transform

Before publishing the Web application or Website, it’s better to make sure that the written transformation code will render as expected or not. So, you can cross check the .config file using Preview Transform.

Let's consider Web.Release.config file to "Preview Transform" the Web.Release.config file. Right-click on the file and select Preview Transform as below,

Transform Web.Config

Preview Transform Output

Transform Web.Config

Publish/deploy the Web application/Web site

If you feel everything transformed as expected in the above screenshot, then it’s time to publish the Web application or Website. Let's publish with Release mode first.

Transform Web.Config

After publishing the Web application or Website, we can notice that the Web.config file has changed with new settings and a new connection strings.

Web.config file.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3.   For more information on how to configure your ASP.NET application, please visit  
  4.   http://go.microsoft.com/fwlink/?LinkId=169433  
  5.   -->  
  6. <configuration>  
  7.   <appsettings>  
  8.     <add key="eMail_From" value="xyz_Release@hotmail.com"/>  
  9.     <add key="eMail_CC" value="xyz_Release@yahoo.com"/>  
  10.     <add key="eMail_BCC" value="xyz_Release@gmail.com"/>  
  11.   </appsettings>  
  12.   <connectionstrings>  
  13.     <add name="Config_Mode" connectionstring="data source=My-PC;initial catalog=MyDB;  
  14.   
  15.       user id=sa;password=test123" providername="System.Data.SqlClient"/>  
  16.   </connectionstrings>  
  17.   <system.web>  
  18.     <compilation targetFramework="4.5" />  
  19.     <httpRuntime targetFramework="4.5" />  
  20.   </system.web>  
  21. </configuration>  

Now, we again deploy/publish with Debug mode.

Transform Web.Config

We will notice that the Web application or Website’s Web.config file has changed with the corresponding Web.Debug.config file configuration.

Web.config file.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3.   For more information on how to configure your ASP.NET application, please visit  
  4.   http://go.microsoft.com/fwlink/?LinkId=169433  
  5.   -->  
  6. <configuration>  
  7.   <appsettings>  
  8.     <add key="eMail_From" value="xyz_Debug@hotmail.com"/>  
  9.     <add key="eMail_CC" value="xyz_Debug@yahoo.com"/>  
  10.     <add key="eMail_BCC" value="xyz_Debug@gmail.com"/>  
  11.   </appsettings>  
  12.   <connectionstrings>  
  13.     <add name="Config_Mode" connectionstring="data source=My-PC;initial catalog=MyDB;  
  14.   
  15.       user id=sa;password=test123" providername="System.Data.SqlClient"/>  
  16.   </connectionstrings>  
  17.   <system.web>  
  18.     <compilation targetFramework="4.5" batch="false" />  
  19.     <httpRuntime targetFramework="4.5" />  
  20.   </system.web>  
  21. </configuration>  

I love this feature. Really, this is a very interesting feature of VS2010 and the above versions. Likewise, there are many more flexible options to transform Web.config files by using the xdt:Locator and xdt:Transform elements.

For more details, please go through the below link:

  • https://msdn.microsoft.com/en-us/library/dd465326.aspx
  • https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations

I would appreciate your valuable comments. Happy coding 😊