Is there any way to access the <compilation />
tag in a web.config file?
有没有办法访问web.config文件中的
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.
我想检查文件中的“debug”属性是否设置为“true”,但我似乎无法弄清楚如何做到这一点。我已经尝试过使用WebConfigurationManager,但这似乎不允许我进入
Update:
更新:
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.
我知道我可以轻松地像XML文档一样加载文件并使用XPath,但我希望框架中已经存在一些可以为我做这件事的东西。似乎会有一些东西,因为已经有方法来获取应用程序设置和连接字符串。
I've also tried using the WebConfigurationManager.GetSection()
method in a few ways:
我也尝试过以下几种方式使用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
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.
所有上述方法都返回null。我假设有一种方法可以进入这个配置部分,因为有一个已经存在的类('CompilationSection'),我只是想不通如何得到它。
7 个解决方案
#1
25
Use:
使用:
using System.Configuration;
using System.Web.Configuration;
...
...
CompilationSection configSection =
(CompilationSection) ConfigurationManager.GetSection( "system.web/compilation" );
You can then check the configSection.Debug
property.
然后,您可以检查configSection.Debug属性。
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.
提示:如果您需要知道如何从配置文件中获取值,请检查\ Windows \ Microsoft.net \ Framework \
#2
22
The easy way to check if you're running in debug mode is to use the HttpContext.IsDebuggingEnabled property. It gets its answer from the compilation element's debug attribute which is the same thing you're trying to do.
检查您是否在调试模式下运行的简单方法是使用HttpContext.IsDebuggingEnabled属性。它从编译元素的调试属性中得到答案,这与您尝试执行的操作相同。
#3
2
After all, you can always load up the Web.config
file into an XmlDocument
and use an XPath
query to find out!
毕竟,您始终可以将Web.config文件加载到XmlDocument中并使用XPath查询来查找!
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Web.config"));
doc.SelectSingleNode("/configuration/system.web/compilation/@debug");
However, I suggest you use the Configuration.GetSection
method for solution.
但是,我建议您使用Configuration.GetSection方法进行解决方案。
CompilationSection section =
Configuration.GetSection("system.web/compilation") as CompilationSection;
bool debug = section != null && section.Debug;
#4
1
You can always check debug option on the compiler level by enclosing your code with
您始终可以通过将代码包含在编译器级别来检查调试选项
#if (DEBUG)
#endif
in case that was the idea...
如果是这个想法......
#5
1
Try using:
尝试使用:
HttpContext.Current.IsDebuggingEnabled
#6
0
Cant you just load the file up as a regular XML File and use XPath to get the nodes?
你不能只是将文件作为常规XML文件加载并使用XPath来获取节点吗?
#7
0
Try using the ConfigurationManager.GetSection method.
尝试使用ConfigurationManager.GetSection方法。
#1
25
Use:
使用:
using System.Configuration;
using System.Web.Configuration;
...
...
CompilationSection configSection =
(CompilationSection) ConfigurationManager.GetSection( "system.web/compilation" );
You can then check the configSection.Debug
property.
然后,您可以检查configSection.Debug属性。
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.
提示:如果您需要知道如何从配置文件中获取值,请检查\ Windows \ Microsoft.net \ Framework \
#2
22
The easy way to check if you're running in debug mode is to use the HttpContext.IsDebuggingEnabled property. It gets its answer from the compilation element's debug attribute which is the same thing you're trying to do.
检查您是否在调试模式下运行的简单方法是使用HttpContext.IsDebuggingEnabled属性。它从编译元素的调试属性中得到答案,这与您尝试执行的操作相同。
#3
2
After all, you can always load up the Web.config
file into an XmlDocument
and use an XPath
query to find out!
毕竟,您始终可以将Web.config文件加载到XmlDocument中并使用XPath查询来查找!
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Web.config"));
doc.SelectSingleNode("/configuration/system.web/compilation/@debug");
However, I suggest you use the Configuration.GetSection
method for solution.
但是,我建议您使用Configuration.GetSection方法进行解决方案。
CompilationSection section =
Configuration.GetSection("system.web/compilation") as CompilationSection;
bool debug = section != null && section.Debug;
#4
1
You can always check debug option on the compiler level by enclosing your code with
您始终可以通过将代码包含在编译器级别来检查调试选项
#if (DEBUG)
#endif
in case that was the idea...
如果是这个想法......
#5
1
Try using:
尝试使用:
HttpContext.Current.IsDebuggingEnabled
#6
0
Cant you just load the file up as a regular XML File and use XPath to get the nodes?
你不能只是将文件作为常规XML文件加载并使用XPath来获取节点吗?
#7
0
Try using the ConfigurationManager.GetSection method.
尝试使用ConfigurationManager.GetSection方法。