【问题标题】:Loading some from config and some other from configSource从 config 加载一些,从 configSource 加载一些
【发布时间】:2016-08-22 12:01:26
【问题描述】:

我有一个带有<appSettings> 标签的配置文件。我希望其中一些保持不变,并从另一个配置文件中加载其中一些。

Web.config:

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

附加设置.config

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

结果: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>

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

<appSettings configSource="Settings.config" />

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

我也试过了

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

但是不行:A section using 'configSource' may contain no other attributes or elements.

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

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

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

【问题讨论】:

    标签: c# asp.net-mvc web-config


    【解决方案1】:

    在这上面浪费了太多时间之后,我来到了灵魂深处:

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

    是非法的,但是:

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

    非常好......所以这一切都归结为将configSource 属性更改为file 属性。现在可以正常使用了。

    【讨论】:

    • 使用 ASP.NET Framework 4.6.1 对我不起作用,它在解析配置文件时出现异常,说“文件”属性是非法的。
    猜你喜欢
    • 2014-05-03
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多