How to: Create Custom Configuration Sections Using ConfigurationSection
Updated: April 2009
You can extend ASP.NET configuration settings with XML configuration elements of your own. To do this, you create a custom configuration section handler. The handler must be a .NET Framework class that inherits from the System.Configuration::ConfigurationSection class. The section handler interprets and processes the settings that are defined in XML configuration elements in a specific section of a Web.config file. You can read and write these settings through the handler's properties.
To create a custom configuration section handler
Create a public class that inherits from the System.Configuration::ConfigurationSection class.
Add code to perform the configuration task or tasks.
The following example shows how to create a handler for a custom configuration section named "PageAppearance." The section has a RemoteOnly attribute and Font and Color elements. The code shows how to use string, integer, and Boolean attributes. It also shows how to use string and integer validators for those attributes. (The Boolean attribute is automatically validated.) The validators check the format of the configuration markup at run time and throw exceptions if the values provided for the custom attributes do not meet the specified criteria.
This example uses the declarative model. A configuration section handler can also be implemented programmatically. For an example, see Classes Used to Create Custom Section Handlers and the System.Configuration::ConfigurationSection class overview.
To add a custom section handler to an ASP.NET configuration file
In the Web.config file, add a sectionGroup element and a section element inside the configSections element, as shown in the following example. The declaration associates the custom section handler with the section name.
Nesting a section element in a sectionGroup is optional, but we recommend doing this to help organize configuration data.
<configuration> <!-- Configuration section-handler declaration area. --> <configSections> <sectionGroup name="pageAppearanceGroup"> <section name="pageAppearance" type="Samples.AspNet.PageAppearanceSection" allowLocation="true" allowDefinition="Everywhere" /> </sectionGroup> <!-- Other <section> and <sectionGroup> elements. --> </configSections> <!-- Configuration section settings area. --> </configuration>
You can add the section-handler declaration in a different configuration file than the one where you add the custom configuration elements, providing that the configuration file where the section handler is declared is higher in the configuration file hierarchy. For more information, see ASP.NET Configuration File Hierarchy and Inheritance.
The assembly file that contains the type defined by the section element must be in the same ASP.NET Web site directory as the Web.config file that references it. The type attribute of the section element must match the manifest of the assembly or a configuration error will be thrown.
Add custom configuration elements in the configuration section settings area of the Web.config file, as shown in the following example:
<configuration> <!-- Configuration section-handler declaration area. --> <!-- Configuration section settings area. --> <pageAppearanceGroup> <pageAppearance remoteOnly="true"> <font name="TimesNewRoman" size="18"/> <color background="000000" foreground="FFFFFF"/> </pageAppearance> </pageAppearanceGroup> <!-- Other configuration settings, such as system.web --> </configuration>