21

I have a value stored in my web.config file that I would like to change when the site is published. I want to change it from TEST to LIVE.

<appSettings>
    <add key="RequestMode" value="TEST" />
    // other keys here
</appSettings>

Is this possible using web.config transformation syntax? If so, how?

Thanks.

dotnetnoob
  • 10,783
  • 20
  • 57
  • 103

4 Answers4

45

Yes this is possible with transformation syntax. This transformation should do the trick:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="RequestMode" value="LIVE" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
  </appSettings>
</configuration>
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
  • how does it know which transformation to use when? is it a file naming convention? As in it knows to use the Web.Debug.config file when its in debug mode and Web.Release.config in Release mode? – user1534664 Mar 06 '14 at 14:09
  • 1
    @user1534664 Yes, it is a naming convention. The Release in Web.Release.config corresponds to the current build configuration. So if you have a custom build configuration named Staging, you would create a Web.Staging.config file. – Erik Schierboom Mar 06 '14 at 14:32
  • 1
    I like this answer as it allows me to specifically replace the keys and not the whole app settings – Ajax3.14 Nov 07 '14 at 21:55
1

This is possible out of the box using Visual Studio 2010. The only caveat is this process is run from within Visual Studio when you use the Publish facilities within it. You won't get the ability to get this transformation from MSBuild (which hampers automated builds).

<appSettings xdt:Transform="Replace">  <add key="ProdKeyA" value="ProdValA"/>  <add key="ProdKeyB" value="ProdValB"/>  <add key="ProdKeyC" value="ProdValC"/></appSettings>

Source: Web.Config Transformations VS 2010

Also ensure your Web.Config.XXXX tranformation file matches your build definition. For instance, debug and release are supported by default so you would need to have a Web.Config.Release to adjust the Web.Config when publishing in release mode.

Frazell Thomas
  • 6,031
  • 1
  • 20
  • 21
  • 1
    Of course you can use the transformation with automated builds. .csproj files are just MSBuild files, after all. See http://nirajrules.wordpress.com/2011/07/04/integrating-web-config-transformations-with-tfs-build/ for more information. – Dan Esparza Jun 25 '13 at 19:34
0

I found the code below at http://mundrisoft.com/tech-bytes/web-config-transformation-for-project-deployment/ , which was working:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="MyDB" 
      connectionString="ReleaseSQLServer" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>
</configuration
0

You can also use the CTT tool for performing web transformation CTT Website You can then after publishing your files, before deployment using powershell can transform the web config (keeping a transform file) and then simply change the name during deployment