![]() |
Web Development »
ASP.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
Add Custom Configuration Sections into a Separate web.configBy Ricky WangThis article introduces a tip of how to add your custom configuration sections in “another” web.config |
C#2.0, Windows, .NET2.0, ASP.NET, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
<appSettings>
<add key="3" value="c"/>
<add key="4" value="d"/>
</appSettings>
<appSettings>
<add key="1" value="a"/>
<add key="2" value="b"/>
</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.
<myCustomGroup>
<myCustomSection myAttrib1="Clowns">
<myChildSection myChildAttrib1="Zippy" myChildAttrib2="Michael Zawondy "/>
</ myCustomSection>
</myCustomGroup>
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!
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.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 8 May 2008 Editor: Deeksha Shenoy |
Copyright 2008 by Ricky Wang Everything else Copyright © CodeProject, 1999-2010 Web10 | Advertise on the Code Project |