Search code examples
c#asp.net-mvcweb-config

Loading some from config and some other from configSource


I have a config file with <appSettings> tag. I want some of them to be constant, and load some of them from another config file.

Web.config:

<appSettings>
  <add key="commonKey1" value="commonValue1" />
  <add key="commonKey2" value="commonValue2" />
  <add key="commonKey3" value="commonValue3" />
  <!-- ??? -->
</appSettings>

AdditionalSettings.config

<appSettings>
  <add key="AdditionalKey1" value="AdditionalValue1" />
  <add key="AdditionalKey2" value="AdditionalValue2" />
</appSettings>

Result: Web.config after compilation should behave like this:

<appSettings>
  <add key="commonKey1" value="commonValue1" />
  <add key="commonKey2" value="commonValue2" />
  <add key="commonKey3" value="commonValue3" />
  <add key="AdditionalKey1" value="AdditionalValue1" />
  <add key="AdditionalKey2" value="AdditionalValue2" />
</appSettings>

It's quite easy if ALL values are stored in separate file :

<appSettings configSource="Settings.config" />

but I have no idea how to merge two files if some tags should be present in base file, and only additional tags should be loaded from a separate one.

I've also tried

<appSettings configSource="Settings.config">
  <add key="commonKey1" value="commonValue1" />
  <add key="commonKey2" value="commonValue2" />
  ... etc

But it won't work: A section using 'configSource' may contain no other attributes or elements.

Of course I can't also just create two tags (one with concrete values and another with configSource), it results in:

There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined

Can anyone help? Is it even possible, or there is another approach to the problem?


Solution

  • After wasting way too much time on this, I came to the soultion:

    <appSettings configSource="Settings.config">
      <add key="commonKey1" value="commonValue1" />
      <add key="commonKey2" value="commonValue2" />
      ... etc
    

    is illegal, but the:

    <appSettings file="Settings.config">
      <add key="commonKey1" value="commonValue1" />
      <add key="commonKey2" value="commonValue2" />
      ... etc
    

    is perfectly fine... So it all came down to changing configSource attribute to file attribute. Now it works fine.