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

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

问题描述

有没有什么办法来访问<编译/> 在web.config文件标签

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

我要检查的调试的属性设置为的真正的的文件中,但我似乎无法弄清楚如何做到这一点。我用试过 WebConfigurationManager ,但这似乎并没有让我去的&LT;编译/&GT; 部分。

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

以上所有的方法都返回。我假设有一种方法去配置部分,因为有一个已经存在( 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 属性。

提示:如果您需要知道如何从一个配置文件中的值,检查machine.config文件在你的 \的Windows \ Microsoft.net \框架\ &LT;版&GT; \ CONFIG 文件夹。在那里,你可以看到所有的配置节处理程序中定义。一旦你知道配置处理程序的名称(在这种情况下,CompilationSection),你可以看它的净文档。

TIP: if you need to know how to get a value from a config file, check the machine.config file in your \Windows\Microsoft.net\Framework\<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天全站免登陆