【问题标题】:Programmatically access the <compilation /> section of a web.config?以编程方式访问 web.config 的 <compilation /> 部分?
【发布时间】:2010-09-28 21:57:48
【问题描述】:

有什么方法可以访问 web.config 文件中的&lt;compilation /&gt; 标签?

我想检查文件中的“debug”属性是否设置为“true”,但我似乎不知道该怎么做.我尝试过使用WebConfigurationManager,但这似乎无法让我进入&lt;compilation /&gt; 部分。

更新:

我知道我可以像 XML 文档一样轻松地加载文件并使用 XPath,但我希望框架中已经有一些东西可以为我做这件事。似乎会有一些东西,因为已经有获取应用程序设置和连接字符串的方法。

我也尝试过以几种方式使用WebConfigurationManager.GetSection() 方法:

WebConfigurationManager.GetSection("compilation")// Name of the tag in the file
WebConfigurationManager.GetSection("CompilationSection") // Name of the class that I'd expect would be returned by this method
WebConfigurationManager.GetSection("system.web") // Parent tag of the 'compilation' tag

以上所有方法都返回null。我假设有一种方法可以访问此配置部分,因为已经存在一个类('CompilationSection'),我只是不知道如何获取它。

【问题讨论】:

  • 您是否将您的部分添加到configSections?类似:&lt;configSections&gt; &lt;section name="compilation" type="xxx" /&gt; ... &lt;/configSections&gt;

标签: c# .net asp.net configuration


【解决方案1】:

毕竟,您始终可以将Web.config 文件加载到XmlDocument 并使用XPath 查询来找出答案!

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Web.config"));     
doc.SelectSingleNode("/configuration/system.web/compilation/@debug");

但是,我建议您使用Configuration.GetSection 方法解决。

CompilationSection section = 
    Configuration.GetSection("system.web/compilation") as CompilationSection;
bool debug = section != null && section.Debug;

【讨论】:

    【解决方案2】:

    您不能将文件作为常规 XML 文件加载并使用 XPath 获取节点吗?

    【讨论】:

      【解决方案3】:

      尝试使用 ConfigurationManager.GetSection 方法。

      【讨论】:

        【解决方案4】:

        用途:

        using System.Configuration;
        using System.Web.Configuration;
        

        ...

          CompilationSection configSection =
                  (CompilationSection) ConfigurationManager.GetSection( "system.web/compilation" );
        

        然后您可以检查configSection.Debug 属性。

        提示:如果您需要知道如何从配置文件中获取值,请检查您的 \Windows\Microsoft.net\Framework\&lt;version&gt;\CONFIG 文件夹中的 machine.config 文件。在那里,您可以看到所有配置节处理程序是如何定义的。一旦您知道配置处理程序的名称(在本例中为 CompilationSection),您就可以在 .Net 文档中查找它。

        【讨论】:

          【解决方案5】:

          检查您是否在调试模式下运行的简单方法是使用HttpContext.IsDebuggingEnabled 属性。它从编译元素的 debug 属性中得到答案,这与您正在尝试做的事情相同。

          【讨论】:

          • 这是当你有一个HttpContext 实例时的最佳答案(即当你在控制器中处理请求时),但如果你需要在应用程序的另一个地方做,就像Global.asax 代码一样,那么@MikeScott 的答案就是您所需要的。
          【解决方案6】:

          您始终可以在编译器级别检查调试选项,方法是用

          括起您的代码
          #if (DEBUG)
          
          #endif
          

          如果是这样的话......

          【讨论】:

          • 这不是 web.config 标志,因为它可以在运行时更改。正如您所写的,这只是编译时间,因此取决于天气是否会进行 DEBUG 或 RELEASE 构建(编译)。这不是这里所要求的。
          【解决方案7】:

          尝试使用:

          HttpContext.Current.IsDebuggingEnabled
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-08-12
            • 1970-01-01
            • 2012-01-06
            • 2010-09-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多