Programmatically access the section of a web.config?

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

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.

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.

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

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Use:

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


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

You can then check the configSection.Debug property.

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.

Method 2

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.

Method 3

After all, you can always load up the Web.config file into an XmlDocument and use an XPath query to find out!

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.
CompilationSection section = 
    Configuration.GetSection("system.web/compilation") as CompilationSection;
bool debug = section != null && section.Debug;

Method 4

You can always check debug option on the compiler level by enclosing your code with

#if (DEBUG)

#endif

in case that was the idea…

Method 5

Try using:

HttpContext.Current.IsDebuggingEnabled

Method 6

Cant you just load the file up as a regular XML File and use XPath to get the nodes?

Method 7

Try using the ConfigurationManager.GetSection method.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x