简体   繁体   English

以编程方式更改web.config:“ buildProviders”部分

[英]Changing web.config programmatically: section “buildProviders”

For deployment, I wrote a tool that modifies some web.config settings from development to production environments. 为了进行部署,我编写了一个工具,可以将某些web.config设置从开发环境修改为生产环境。 That works well except in one case: 除一种情况外,这种方法效果很好:

When I try to set debug to false , my buildProviders section disappears. 当我尝试将debug设置为false时 ,我的buildProviders部分消失了。

This is my system.web section: 这是我的system.web部分:

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

and this is my modification code: 这是我的修改代码:

    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();
    }

In the resulting web.config the buildProviders section is missing. 在生成的web.config中,缺少buildProviders部分。

In the debugger I can see that the comp.BuildProviders collection is empty. 在调试器中,我可以看到comp.BuildProviders集合为空。 However, a protected property base.Items contains two elements with _entryType = Removed and _key = ".resx" resp. 但是,受保护的属性base.Items包含两个元素,分别具有_entryType =已删除_key =“ .resx” _key = ".resources" . _key =“ .resources”

How can I manage that both "remove" elements will be saved to web.config ? 如何管理两个“删除”元素都将保存到web.config中

Finally I solved my problem using classes of the System.Xml Namespace (XmlDocument, XmlElement etc.) instead of thje System.Configuration namespace. 最后,我使用System.Xml命名空间(XmlDocument,XmlElement等)的类而不是System.Configuration命名空间解决了我的问题。

This works fine. 这很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM