【问题标题】:Changing web.config programmatically: section "buildProviders"以编程方式更改 web.config:“buildProviders”部分
【发布时间】:2016-08-12 19:35:23
【问题描述】:

对于部署,我编写了一个工具,可以将一些 web.config 设置从开发环境修改为生产环境。除了一种情况外,这种方法效果很好:

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

这是我的 system.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".

如何管理将两个“删除”元素都保存到 web.config

【问题讨论】:

  • 为什么不在项目文件中使用TransformXML任务?请在此处查看可能的解决方案:visualstudiogallery.msdn.microsoft.com/… 或在此处使用手动转换:johan.driessen.se/posts/…
  • 原因是我已经有了一种机制,可以通过特殊的网页在运行时修改一些 web.config 设置(例如,打开/关闭 ASP.Net 跟踪)。我想我也可以使用这种机制来打开/关闭调试。 (在某些情况下,我希望在异常堆栈跟踪中包含行号。)

标签: c# configuration web-config


【解决方案1】:

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

这很好用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    相关资源
    最近更新 更多