Click here to Skip to main content
15,881,776 members
Articles / Web Development / ASP.NET
Article

Add Custom Configuration Sections into a Separate web.config

Rate me:
Please Sign up or sign in to vote.
3.91/5 (6 votes)
8 May 2008CPOL3 min read 164.9K   328   28   6
This article introduces a tip of how to add your custom configuration sections in “another” web.config

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.

Image 1

Figure 1. Put a web.config to a specified directory


Listing 1. Open a web.config under a Specified Directory

XML
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

XML
<appSettings>
<add key="3" value="c"/>
<add key="4" value="d"/>
</appSettings>

Listing 3. <appSettings> of web.config under Root Directory

XML
<appSettings>
<add key="1" value="a"/>
<add key="2" value="b"/>
</appSettings>

Image 2

Figure 2. Number of AppSettings

Borrowed from example codes within “How to: Create Custom Configuration Sections Using ConfigurationSection1, 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

XML
<myCustomGroup>
<myCustomSection myAttrib1="Clowns">
        <myChildSection myChildAttrib1="Zippy" myChildAttrib2="Michael Zawondy "/>
        </ myCustomSection>
</myCustomGroup>

Listing 5. Retrieve myCustomSection Section

XML
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!

Image 3
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

  1. How to: Create Custom Configuration Sections Using ConfigurationSection
  2. Custom Configuration Sections in 3 Easy Steps
  3. Referencing external config files from Web.Config

History

  • 8th May, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer ITCAC(數位協進)
Taiwan Taiwan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSeparating config section best practice Pin
Kin Tutnik30-Jun-10 15:23
Kin Tutnik30-Jun-10 15:23 
GeneralconfigSource [modified] Pin
Shaggy Wolf13-May-08 6:06
Shaggy Wolf13-May-08 6:06 
GeneralRe: configSource Pin
Ricky Wang13-May-08 14:48
Ricky Wang13-May-08 14:48 
GeneralRe: configSource Pin
Shaggy Wolf14-May-08 3:42
Shaggy Wolf14-May-08 3:42 
GeneralRe: configSource Pin
Ricky Wang17-May-08 3:03
Ricky Wang17-May-08 3:03 
GeneralRe: configSource Pin
mbaocha29-Apr-09 13:18
mbaocha29-Apr-09 13:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.