c# - 尽管定义了文件属性,但键值对已添加到 web.config 文件中

标签 c# asp.net configuration web-config

 Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
  config.SectionGroups.Add("testing", new ConfigurationSectionGroup());         
  ConfigurationSectionGroup testing = config.SectionGroups["testing"];
  testing.Sections.Add("subsec", new AppSettingsSection() {File="tst.config" });
  KeyValueConfigurationCollection settings = ((AppSettingsSection)testing.Sections["subsec"]).Settings;

  settings.Add("tst", "ok");
config.Save();

结果是:

    <sectionGroup name="testing" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
      <section name="subsec" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </sectionGroup>

  <testing>
    <subsec file="tst.config">
      <add key="tst" value="ok" /> //this should not be here but must be added to the tst.config file
    </subsec>
  </testing>

为什么不将设置添加到文件中?

最佳答案

您应该能够以编程方式设置配置源(而不是文件),如下所示:

ConfigurationSection subsec = config.GetSection("testing/subsec");
subsec.SectionInformation.ConfigSource = "tst.config";

关于c# - 尽管定义了文件属性,但键值对已添加到 web.config 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24341050/

相关文章:

c# - C# 中的打开文件对话框

c# - 如何强制所有实现在其构造函数中调用特定方法

c# - 如何从 .js 文件访问 asp 标记中的 js 变量(两者都在同一页面上执行)?

c# - 如何从模型中获取图像以在 CSS 样式标签的 View 中使用?

configuration - FFmpeg:阅读源代码,我如何理解给定配置选项的影响?

c# - 当声明为函数的返回值时,C# 结构是否曾经装箱?

c# - 如何避免重复代码?

c# - 如何创建可枚举/类字符串或 int 并序列化?

ELASTICSEARCH:脑裂

asp.net - 将设置移动到另一个配置文件