【问题标题】:Is it possible for <appSettings /> to contain settings AND refer to another <appSettings /> section in a different file?<appSettings /> 是否可以包含设置并引用另一个文件中的另一个 <appSettings /> 部分?
【发布时间】:2012-07-25 23:06:42
【问题描述】:

是否可以在我的 app.config 中有一个 &lt;appSettings /&gt; 部分,其中包含许多设置,但它还引用了不同文件中的 &lt;appSettings /&gt; 部分?

这将允许我保留只有开发人员应该感兴趣的配置选项,例如在主窗口上显示调试输出的选项(非常混乱但对我很有用)或将某些文件保存到特定位置。

具体来说,这是我想要实现的目标:

<!-- this is the appSettings section of the normal app.config file,
     which also contains everything else that app.config would -->
<appSettings configSource="AppSettings.config"> <!-- references another file -->
  <add key="deployment_config_option1" value="1"/>
  <add key="deployment_config_option2" value="2"/>
</appSettings>

<!-- this a new appSettings section of another file called DevSettings.Config
     which only contains settings us developers are interested in
     by keeping these settings in a different file, 
     I can ensure it's never deployed -->
<appSettings> <!-- references another file -->
  <add key="show_debug_on_screen" value="true"/>
  <add key="save_copy_to_desktop" value="false"/>
</appSettings>

【问题讨论】:

  • 不是重复的。这个问题比我的更笼统。
  • 基本上是一样的。您希望您的 app.config 包含其他配置。如果您不想部署第二个配置,您只需确保删除已部署的 app.config 中包含的应用配置。

标签: .net visual-studio-2010 app-config


【解决方案1】:

是的。考虑到可能的场景数量,doc 有点薄,但根据下面的引用,只有在 appSettings 标记的情况下才会合并。

由于对 Web.config 文件的任何更改都会导致应用程序重新启动,因此使用单独的文件允许用户修改 appSettings 部分中的值,而不会导致应用程序重新启动。 单独文件的内容与 Web.config 文件中的 appSettings 部分合并。此功能仅限于 appSettings 属性。

在 ASP.NET 的情况下对此(如下)的测试表明它可以使用 &lt;add&gt; 标记。在从属文件中使用像&lt;clear&gt; 标签这样的花哨的东西显然会出现问题,尽管我没有测试这种边缘情况。不会说 ASP 以外的部署技术; Machine.Config 也没有经过测试。

在 web.config 文件中:

    <appSettings file="_config\AppSettings.Config">
            <add key="testing-1" value="Web.Config" />
    </appSettings>

在“_config\AppSettings.config”文件中:

    <appSettings>
            <add key="testing-2" value="_config/AppSettings.config" />
    </appSettings>

【讨论】:

    猜你喜欢
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多