以编程方式更改web.config:section";buildProviders”;

本文关键字:quot buildProviders section 方式更 编程 web config | 更新日期: 2024-10-21 14:19:54

为了部署,我编写了一个工具,可以修改从开发环境到生产环境的一些web.config设置。除了一种情况外,它运行良好:

当我尝试将debug设置为false时,我的buildProviders部分将消失。

这是我的系统.web部分:

  <system.web>
    <machineKey ... />
    <compilation debug="true" strict="false" targetFramework="4.0">
      <buildProviders>
        <remove extension=".resx"/>
        <remove extension=".resources"/>
      </buildProviders>
      <assemblies>
            ...
      </assemblies>
    </compilation>

这是我的修改代码:

    private void ChangeDebugSetting()
    {
        System.Configuration.Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
        System.Web.Configuration.CompilationSection comp = (System.Web.Configuration.CompilationSection)configuration.GetSection("system.web/compilation");
        comp.Debug = false;
        configuration.Save();
    }

在生成的web.config中,缺少buildProviders部分。

在调试器中,我可以看到comp.BuildProviders集合为空。但是,受保护的属性base.Items包含两个分别为_entryType=Removed_key=".resx"的元素_key=".resources".

如何管理这两个"remove"元素都将保存到web.config

以编程方式更改web.config:section";buildProviders”;

最后,我使用System.Xml命名空间的类(XmlDocument、XmlElement等)而不是System.Configuration命名空间来解决问题。

这很好用。