Web config transform for custom section

Ctrl_Alt_Defeat picture Ctrl_Alt_Defeat · Mar 7, 2015 · Viewed 7.4k times · Source

I have numerous different Web.configs in my MVC 5 Application for different environments - e.g. Test/Prod

I have web transforms in place to change values for the different environments. So for example I have the following app setting in my web.config file:

<appSettings>
<add key="DevDisplayPanel" value="true" />
</appSettings>

Then in my Web.Test.config and Web.Prod.config using a web transform to change the value as below:

<appSettings>
<add key="DevDisplayPanel" 
     xdt:Transform="Replace" 
     xdt:Locator="Match(key)" 
     value="false" />
<appSettings>

However in my Web.config I also have my own custom section which is outside the <appSettings> section and is as below:

  <myCustomSection>
    <serverList>
      <add zone="Zone1" url="https://dev-myurl1.com"/>
      <add zone="Zone2" url="https://dev-myurl2.com"/>
      <add zone="Zone2" url="https://dev-myurl3.com"/>
    </serverList>
  </myCustomSection>

My question is - is it possible to have a web transform so that for Test and Prod would look as below:

Test:

  <myCustomSection>
    <serverList>
      <add zone="Zone1" url="https://test-myurl1.com"/>
      <add zone="Zone2" url="https://test-myurl2.com"/>
      <add zone="Zone2" url="https://test-myurl3.com"/>
    </serverList>
  </myCustomSection>

Prod:

  <myCustomSection>
    <serverList>
      <add zone="Zone1" url="https://prod-myurl1.com"/>
      <add zone="Zone2" url="https://prod-myurl2.com"/>
      <add zone="Zone2" url="https://prod-myurl3.com"/>
    </serverList>
  </myCustomSection>

Answer

Darin Dimitrov picture Darin Dimitrov · Mar 7, 2015

You can try replacing the contents of the <serverList> tag.

Test:

<myCustomSection>
    <serverList xdt:Transform="Replace">
        <add zone="Zone1" url="https://test-myurl1.com"/>
        <add zone="Zone2" url="https://test-myurl2.com"/>
        <add zone="Zone2" url="https://test-myurl3.com"/>
    </serverList>
</myCustomSection>

Prod:

<myCustomSection>
    <serverList xdt:Transform="Replace">
        <add zone="Zone1" url="https://prod-myurl1.com"/>
        <add zone="Zone2" url="https://prod-myurl2.com"/>
        <add zone="Zone2" url="https://prod-myurl3.com"/>
    </serverList>
</myCustomSection>