以编程方式访问<编译/>web.config 的一部分? [英] Programmatically access the <compilation /> section of a web.config?

查看:14
本文介绍了以编程方式访问<编译/>web.config 的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法访问 web.config 文件中的 <compilation/> 标签?

Is there any way to access the <compilation /> tag in a web.config file?

我想检查文件中的debug"属性是否设置为true",但我似乎不知道该怎么做.我已经尝试使用 WebConfigurationManager,但这似乎不允许我进入 <compilation/> 部分.

I want to check if the "debug" attribute is set to "true" in the file, but I can't seem to figure out how to do it. I've tried using the WebConfigurationManager, but that doesn't seem to allow me to get to the <compilation /> section.

更新:

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

I know I could easily just load the file like an XML Document and use XPath, but I was hoping there was already something in the framework that would do this for me. It seems like there would be something since there are already ways to get App Settings and Connection Strings.

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

I've also tried using the WebConfigurationManager.GetSection() method in a few ways:

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'),我就是不知道如何获取它.

All of the above methods return null. I'm assuming there is a way to get to this configuration section since there is a class that already exists ('CompilationSection'), I just can't figure out how to get it.

推荐答案

使用:

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

...

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

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

You can then check the configSection.Debug property.

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

TIP: if you need to know how to get a value from a config file, check the machine.config file in your WindowsMicrosoft.netFramework<version>CONFIG folder. In there you can see how all the configuration section handlers are defined. Once you know the name of the config handler (in this case, CompilationSection), you can look it up in the .Net docs.

这篇关于以编程方式访问&lt;编译/&gt;web.config 的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆