Click here to Skip to main content
13,553,262 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 8 May 2008
Licenced CPOL

Add Custom Configuration Sections into a Separate web.config

, 8 May 2008
Rate this:
Please Sign up or sign in to vote.
This article introduces a tip of how to add your custom configuration sections in “another” web.config


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.


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.


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</strong />. 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

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

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

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

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

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

Listing 5. Retrieve myCustomSection Section

Configuration config =  WebConfigurationManager.OpenWebConfiguration
    (System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "/MyConfig");
MyConfigSectionHandler.MyHandler myHandler =  

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


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.


  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


  • 8th May, 2008: Initial post


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

You may also be interested in...

Comments and Discussions

GeneralSeparating config section best practice Pin
Kin Tutnik30-Jun-10 15:23
memberKin Tutnik30-Jun-10 15:23 
GeneralconfigSource [modified] Pin
Shaggy Wolf13-May-08 6:06
memberShaggy Wolf13-May-08 6:06 
GeneralRe: configSource Pin
Ricky Wang13-May-08 14:48
memberRicky Wang13-May-08 14:48 
GeneralRe: configSource Pin
Shaggy Wolf14-May-08 3:42
memberShaggy Wolf14-May-08 3:42 
GeneralRe: configSource Pin
Ricky Wang17-May-08 3:03
memberRicky Wang17-May-08 3:03 
GeneralRe: configSource Pin
mbaocha29-Apr-09 13:18
membermbaocha29-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 8 May 2008
Article Copyright 2008 by Ricky Wang
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid