Introduction
Adding custom configuration sections lets you leverage built-in .NET configuration mechanism to employ strong-type objects to access the configuration content without taking care of manipulation of XML1, 2. This article introduces a tip about how to add your custom configuration sections in “another” web.config while not losing the support as in the web.config located at the root of Web applications.
Background
Not so long ago, I asked a question on this thread, regarding how to place my defined configuration section into other files for managing configuration contents with ease. As can be imagined easily, the web.config has already defined a large number of configuration sections; stacking my own custom configuration section onto it will further clutter its structure.
I got a reply about extending <appSettings>
section to another files, but this is not enough because many of my settings should be logically organized as an individual section. This issue appears to be unresolved (at least based on Googling), so I would like to share my workaround with you.
Implementation
Calling WebConfigurationManager.OpenWebConfiguration will return a Configuration instance by which you can retrieve various sections within a web.config. You can employ this to open a web.config under a specified directory such as MyConfig (Figure 1) as in Listing 1. System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath
will be replaced with the relative path to your root of Web application like /WebApp1.
Figure 1. Put a web.config to a specified directory
Listing 1. Open a web.config under a Specified Directory
Configuration config = WebConfigurationManager.OpenWebConfiguration
(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "/MyConfig");
To experiment the behavior loading a web.config under MyConfig directory, two setting items are added to <appSettings>
shown as Listing 2. And there have been two settings in <appSettings>
of web.config under root shown as Listing 3. Using debugger to watch the config
variable in Listing 1, we can observe that there are four setting items within config.AppSettings.Settings
(Figure 2). In other words, the content of web.config under MyConfig
will be merged into the default one under root. Hence, I conjecture we can define our custom section in a separate web.config.
Listing 2. <appSettings> of web.config under MyConfig Directory
<appSettings>
<add key="3" value="c"/>
<add key="4" value="d"/>
</appSettings>
Listing 3. <appSettings> of web.config under Root Directory
<appSettings>
<add key="1" value="a"/>
<add key="2" value="b"/>
</appSettings>
Figure 2. Number of AppSettings
Borrowed from example codes within “How to: Create Custom Configuration Sections Using ConfigurationSection”1, we add a myCustomSection
section in the web.config under MyConfig
as in Listing 4, and create MyHandler
class in App_Code
so that we can access this section with this class, which is shown in Listing 5. The GetSection
method lets you retrieve the instance represented by a specified section. If you cannot catch on what I am talking about, please see the references.
Listing 4. Custom Section in web.config under MyConfig
<myCustomGroup>
<myCustomSection myAttrib1="Clowns">
<myChildSection myChildAttrib1="Zippy" myChildAttrib2="Michael Zawondy "/>
</ myCustomSection>
</myCustomGroup>
Listing 5. Retrieve myCustomSection Section
Configuration config = WebConfigurationManager.OpenWebConfiguration
(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "/MyConfig");
MyConfigSectionHandler.MyHandler myHandler =
(MyConfigSectionHandler.MyHandler)config.GetSection("myCustomGroup/myCustomSection");
Now let's use debugger to prove the idea. As can be observed in Figure 3, the configuration content defined at Listing 4 becomes the sub-properties of myHandler
. You can alter MyChildAttribute1
to some value, says “Ooo”, and then call config.Save()
to update the value of MyChildAttribute1
in the web.config under MyConfig
. Pretty convenient!
Figure 3. Debug myHander variable
Conclusion
In this article, we introduce a tip to let you put your custom configuration into a separate web.config without losing the support of the .NET configuration Framework. The only limitation from my perspective is that you must name the configuration file web.config, which may not be appropriate, and hence you cannot put it under the root directory of your Web application.
Reference
- How to: Create Custom Configuration Sections Using ConfigurationSection
- Custom Configuration Sections in 3 Easy Steps
- Referencing external config files from Web.Config
History
- 8th May, 2008: Initial post