Click here to Skip to main content
Click here to Skip to main content

Add Custom Configuration Sections into a Separate web.config

By , 8 May 2008
Rate this:
Please Sign up or sign in to vote.

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

<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

  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)

About the Author

Ricky Wang
Software Developer ITCAC(數位協進)
Taiwan Taiwan
No Biography provided

Comments and Discussions

 
GeneralSeparating config section best practice PinmemberKin Tutnik30-Jun-10 15:23 
GeneralconfigSource [modified] PinmemberShaggy Wolf13-May-08 6:06 
GeneralRe: configSource PinmemberRicky Wang13-May-08 14:48 
GeneralRe: configSource PinmemberShaggy Wolf14-May-08 3:42 
GeneralRe: configSource PinmemberRicky Wang17-May-08 3:03 
Hi, Shaggy
I am not sure if I fully understnad what you mean. Do you mean the configuration sections support ConfigureSource attribute?
 
Reagrds,
Ricky.
 
Take nothing on looks; take everything on evidence.
- Charles Dickens

GeneralRe: configSource Pinmembermbaocha29-Apr-09 13:18 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 8 May 2008
Article Copyright 2008 by Ricky Wang
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid