Click here to Skip to main content
15,885,244 members
Articles / Web Development / ASP.NET
Tip/Trick

Transform Web.Config when Deploying a Web Application Project

Rate me:
Please Sign up or sign in to vote.
4.85/5 (12 votes)
11 Mar 2013CPOL2 min read 88.7K   454   15   2
Transform Web.Config when deploying a Web Application Project.

Introduction

One of the really cool features that are integrated with Visual Studio 2010 is Web.Config (XDT) transformations. ASP.NET Web.Config transformations are a great way to manage configurations in several environments. We can easily change a database connection string or other settings within our Web.config file when  deploying a Web Application Project. When we deploy a Web site, we often want some settings in the deployed application's Web.config file to be different from the development Web.config file. For example, we might want to change the connection strings, security settings, active directory and network addresses, proxy settings, service endpoints and so on. Not only change the connection string or other settings but also insert new settings, remove existing settings and so on during deployment. Here is the Web.Config(XDT) transformations syntax documentation on MSDN.

Background 

A Web.config transformation file contains XML markup that specifies how to change the Web.config file when it is deployed.

  • It only works for web applications. So firstly we have to create a Web Application Project . 
  • After creating  the project Right click on web.config and select Add Config Transforms.
  • Image 1

  • When you click Add Config Transforms - this will create the default web.debug.config and web.Release.config files.
  • Image 2

The root element of a transform file must specify the XML-Document-Transform namespace in its opening tag. I used some features form ML-Document-Transform that I needed.

Web.Release.Config

XML
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">  
  <appsettings>
    <add key="Email" value="mmoin56@yahoo.com" 
      xdt:locator="Match(key)" xdt:transform="RemoveAll">
    <add key="EMailFrom" value="mmoin56@hotmail.com"/>    
    <add key="EMailCC" value="mmoin56@yahoo.com"/>
    <add key="EMailBCC" value="mmoin56@gmail.com" 
      xdt:locator="Match(key)" xdt:transform="Insert"/>
 </appsettings> 
  
  <connectionstrings xdt:transform="Replace">
    <add name="Release-Mode" connectionstring="data source=MOIN-PC;initial catalog=MasterCareEMR; 
      user id=sa;password=123" providername="System.Data.SqlClient"/>
   </connectionstrings>
  
  <system.web>
    <compilation xdt:transform="RemoveAttributes(debug)"/>
    <compilation batch="false" xdt:transform="SetAttributes(batch)"/>
    <authentication mode="Windows" xdt:transform="SetAttributes(mode)"/>
  </system.web>
</configuration>

Web.Debug.Config

XML
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appsettings>
    <add key="Email" value="mmoin56@yahoo.com" 
      xdt:locator="Match(key)" xdt:transform="RemoveAll"/>
    <add key="EMailFrom" value="mmoin56@hotmail.com"/>
    <add key="EMailCC" value="mmoin56@yahoo.com"/>
    <add key="EMailBCC" value="mmoin56@gmail.com" 
      xdt:locator="Match(key)" xdt:transform="Insert"/>
  </appsettings>
  
  <connectionstrings xdt:transform="Replace">
    <add name="Debug-mode" connectionstring="data source=MOIN-PC;initial 
      catalog=MasterCareEMR; user id=sa;password=123" providername="System.Data.SqlClient"/>
  </connectionstrings>
  <system.web>
    <compilation batch="false" xdt:transform="SetAttributes(batch)"/>
    <authentication mode="Windows" xdt:transform="SetAttributes(mode)"/>
    <customerrors mode="RemoteOnly" defaultredirect="GenericErrorPage.htm" 
          xdt:locator="Match(mode)" xdt:transform="Insert">
      <error statuscode="403" redirect="NoAccess.htm"/>
      <error statuscode="404" redirect="FileNotFound.htm"/>
    </customerrors>    
 </system.web>
</configuration>

Remove all existing values by using xdt:Locator="Match(key)" xdt:Transform="RemoveAll", and then xdt:Transform="Insert", this will ensure any existing values are removed and inserted with the new ones. Also xdt:Transform="Replace" will replace any existing elements. In addition we can use SetAttributes and RemoveAttributes to add new attributes or remove existing attributes.

Default Web.Config file

XML
<configuration>
  <appsettings>
    <add value="mmoin56@yahoo.com" key="Email"/>
    <add value="mmoin56@hotmail.com" key="EMailFrom"/>
    <add value="mmoin56@yahoo.com" key="EMailCC"/>
  </appsettings>

  <connectionstrings>
    <add providername="System.Data.SqlClient" 
      connectionstring="data source=MOIN-PC;initial catalog=MasterCareEMR; 
         user id=sa;password=123" name="MCEMRBPPConnectionString"/>
  </connectionstrings>

  <system.web>
    <compilation targetframework="4.0" batch="true" debug="true"/>
    <authentication mode="Windows"/> 
 </system.web>
</configuration>

Now we will publish the Project with Release mode.

Image 3

After publishing the Project we notice that the Web.config file has changed with new settings and a new connection string.

Web.config file.

XML
<configuration>
  <appsettings>
    <add value="mmoin56@hotmail.com" key="EMailFrom"/>
    <add value="mmoin56@yahoo.com" key="EMailCC"/>
    <add value="mmoin56@gmail.com" key="EMailBCC"/>
  </appsettings>

  <connectionstrings>
    <add providername="System.Data.SqlClient" connectionstring="data source=MOIN-PC;initial 
      catalog=MasterCareEMR; user id=sa;password=123" name="Release-Mode"/>
  </connectionstrings>

  <system.web>
    <compilation targetframework="4.0" batch="false"/>
    <authentication mode="Forms"/>
  </system.web>
</configuration>

If we again publish with Debug mode we will notice that the Web.config has changed the corresponding Web.Debug.config file configuration. Really it is a very interesting feature of VS2010.

Conclusion

There are many flexible options to transform Web.config files using the xdt:Transform and xdt:Locator elements and it is very easy. I hope this will make your life a little easier.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) IT Consultants Ltd (QCash).
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
reza assar28-Oct-16 19:09
reza assar28-Oct-16 19:09 
GeneralMy vote of 5 Pin
Tareq Newaz Shahriar9-Jun-13 7:15
Tareq Newaz Shahriar9-Jun-13 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.