283

Is it possible to transform the following Web.config appSettings file:

<appSettings>
    <add key="developmentModeUserId" value="00297022" />
    <add key="developmentMode" value="true" />
    <!-- other settings here that should stay -->
</appSettings>

into something like this:

<appSettings>
    <add key="developmentMode" value="false" />
    <!-- other settings here that should stay -->
</appSettings>

So, I need to remove the key developmentModeUserId, and I need to replace the value for the key developmentMode.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
dragonfly
  • 17,407
  • 30
  • 110
  • 219

4 Answers4

489

You want something like:

<appSettings>
  <add key="developmentModeUserId" xdt:Transform="Remove" xdt:Locator="Match(key)"/>
  <add key="developmentMode" value="false" xdt:Transform="SetAttributes"
          xdt:Locator="Match(key)"/>
</appSettings>

See Also: Web.config Transformation Syntax for Web Application Project Deployment

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Joe
  • 122,218
  • 32
  • 205
  • 338
  • 28
    Note the keys are case-sensitive! – Cosmin Oct 03 '13 at 17:10
  • 2
    Excellent answer. I was trying 3rd party options like Slow Cheetah and getting nowhere - this was simple and perfect. – Steve Aug 14 '15 at 20:53
  • 2
    @stevens: You'd need Slow Cheetah if you want to transform, say, app.config files for native applications. The syntax, however, should be identical if I recall (it's been a while since I had to use Slow Cheetah). – Ellesedil Apr 11 '16 at 21:45
  • an alternative to slow cheater is to create myotherconfig.staging.config and in the web.Staging.config transform, transform the reference to myotherconfig.config in the web.config – nologo Oct 04 '17 at 23:40
  • https://weblogs.asp.net/srkirkland/common-web-config-transformations-with-visual-studio-2010 – Joe Jan 18 '18 at 16:42
  • 4
    Just a minor clarification that might help some people: the `xdt:Transform="SetAttributes" xdt:Locator="Match(key)"` part should be used in the Web.Whatever.config and not in the Web.config file. – user3533716 Jun 26 '18 at 14:40
  • @user3533716 so if the snippet above goes in the web.debug.config file, what happens to the web.config file in correspondence of those keys? – Zizzipupp Nov 29 '19 at 12:20
14

Replacing all AppSettings

This is the overkill case where you just want to replace an entire section of the web.config. In this case I will replace all AppSettings in the web.config will new settings in web.release.config. This is my baseline web.config appSettings:

<appSettings>
  <add key="KeyA" value="ValA"/>
  <add key="KeyB" value="ValB"/>
</appSettings>

Now in my web.release.config file, I am going to create a appSettings section except I will include the attribute xdt:Transform=”Replace” since I want to just replace the entire element. I did not have to use xdt:Locator because there is nothing to locate – I just want to wipe the slate clean and replace everything.

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

Note that in the web.release.config file my appSettings section has three keys instead of two, and the keys aren’t even the same. Now let’s look at the generated web.config file what happens when we publish:

<appSettings>
   <add key="ProdKeyA" value="ProdValA"/>
   <add key="ProdKeyB" value="ProdValB"/>
   <add key="ProdKeyC" value="ProdValC"/>
 </appSettings>

Just as we expected – the web.config appSettings were completely replaced by the values in web.release config. That was easy!

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Joe
  • 509
  • 4
  • 11
  • If you're coming from a system that used separate, whole web.configs for different environments.... this has to be the easiest implementation. You can even replace the whole configuration node with – jaybro Nov 23 '20 at 22:22
11

If you want to make transformation your app setting from web config file to web.Release.config,you have to do the following steps. Let your web.config app setting file is this-

<appSettings>
     <add key ="K1" value="Debendra Dash"/>
  </appSettings>

Now here is the web.Release.config for the transformation.

<appSettings>
    <add key="K1" value="value dynamicly from Realease"
       xdt:Transform="SetAttributes"
          xdt:Locator="Match(key)"   
         />
  </appSettings>

This will transform the value of K1 to the new value in realese Mode.

Debendra Dash
  • 5,334
  • 46
  • 38
2

I do not like transformations to have any more info than needed. So instead of restating the keys, I simply state the condition and intention. It is much easier to see the intention when done like this, at least IMO. Also, I try and put all the xdt attributes first to indicate to the reader, these are transformations and not new things being defined.

<appSettings>
  <add xdt:Locator="Condition(@key='developmentModeUserId')" xdt:Transform="Remove" />
  <add xdt:Locator="Condition(@key='developmentMode')" xdt:Transform="SetAttributes"
       value="false"/>
</appSettings>

In the above it is much easier to see that the first one is removing the element. The 2nd one is setting attributes. It will set/replace any attributes you define here. In this case it will simply set value to false.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64