从config中加载一些,从configSource加载其他一些

Kam*_*l T 7 c# asp.net-mvc web-config

我有一个带<appSettings>标签的配置文件.我希望它们中的一些是常量,并从另一个配置文件加载其中一些.

Web.config文件:

<appSettings>
  <add key="commonKey1" value="commonValue1" />
  <add key="commonKey2" value="commonValue2" />
  <add key="commonKey3" value="commonValue3" />
  <!-- ??? -->
</appSettings>
Run Code Online (Sandbox Code Playgroud)

AdditionalSettings.config

<appSettings>
  <add key="AdditionalKey1" value="AdditionalValue1" />
  <add key="AdditionalKey2" value="AdditionalValue2" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

结果:Web.config编译后应该像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

如果所有值都存储在单独的文件中,这很容易:

<appSettings configSource="Settings.config" />
Run Code Online (Sandbox Code Playgroud)

但是我不知道如果基本文件中应该存在某些标签,如何合并两个文件,并且只应从单独的标签加载其他标签.

我也试过了

<appSettings configSource="Settings.config">
  <add key="commonKey1" value="commonValue1" />
  <add key="commonKey2" value="commonValue2" />
  ... etc
Run Code Online (Sandbox Code Playgroud)

但它不会起作用: A section using 'configSource' may contain no other attributes or elements.

当然我也不能只创建两个标签(一个具有具体值,另一个具有configSource),它导致:

There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' section defined
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?它是否可能,或者还有另一种解决问题的方法?

Kam*_*l T 7

在浪费了太多时间之后,我来到了灵魂:

<appSettings configSource="Settings.config">
  <add key="commonKey1" value="commonValue1" />
  <add key="commonKey2" value="commonValue2" />
  ... etc
Run Code Online (Sandbox Code Playgroud)

是非法的,但是:

<appSettings file="Settings.config">
  <add key="commonKey1" value="commonValue1" />
  <add key="commonKey2" value="commonValue2" />
  ... etc
Run Code Online (Sandbox Code Playgroud)

完全没问题...所以这一切都归结为将configSource属性更改为file属性.现在它工作正常.

  • 使用ASP.NET Framework 4.6.1不适用于我,它在解析配置文件时给出了一个异常,指出“文件”属性是非法的。 (2认同)
  • 也不在.NET Framework 4.7上工作 (2认同)