12.5. Accessing Other web.config Configuration Elements
You want to be able to read application information from a web.config file that is unavailable as an
key/value pair but present as an attribute or child element of some other element of the file.
OpenWebConfiguration method of the
WebConfigurationManager object to read the web.config file into a
Configuration object, and use the
GetSection method to access the desired section.
In the code-behind class for your ASP.NET page, use the .NET language of your choice to:
OpenWebConfigurationmethod of the
WebConfigurationManagerobject to read the web.config file into a
GetSectionmethod to access the desired section.
Cast the returned
ConfigurationSectionobject to the type of the object for the section being accessed.
Use the properties of the object to access the desired information.
Examples 12-6, 12-7 through 12-8 show an application we’ve written that implements this solution and retrieves attribute settings from the
<trace> element of a web.config file. Example 12-6 shows the .aspx file that displays the information. Examples 12-7 (VB) and 12-8 (C#) show the code-behind class for the page that does the work of reading the settings from the
In ASP.NET 1.x, you had to resort to opening the web.config file using an
XmlDocument object to access many of the configuration elements. In ASP.NET 2.0, new classes have been introduced to read ...