Creating New Configuration Sections

You can extend the standard set of ASP.NET configuration settings with XML configuration tags of your own. To do this, you must create your own configuration section handler. The handler must be a .NET Framework class that implements the IConfigurationSectionHandler interface. The section handler interprets and processes the settings defined in XML tags within a specific portion of a Web.config file and returns an appropriate configuration object based on the configuration settings. The configuration object that the handler class returns can be any data structure; it is not limited to any base configuration class or configuration format.

The following example defines an IConfigurationSectionHandler interface.

Namespace System.Web.Configuration 
   Public Interface IConfigurationSectionHandler 
      Function Create(parent As Object, input As Object, _
         node As XmlNode) As Object
   End Interface
End Namespace
[C#]
namespace System.Web.Configuration 
{
   public interface IConfigurationSectionHandler 
   {
      public Object Create(Object parent, Object input, 
         XmlNode node);
   }
}

You can also define your own section that uses the same configuration handler as the <appSettings> section. For example:

<configuration>
   <configSections>
      <sectionGroup name="myGroup">
         <sectionGroup name="nestedGroup">
            <section name="mySection" type=
               "System.Configuration.NameValueSectionHandler,System"/>
         </sectionGroup>
      </sectionGroup>
   </configSections>

   <myGroup>
      <nestedGroup>
         <mySection>
            <add key="key_one" value="1"/>
            <add key="key_two" value="2"/>
         </mySection>
      </nestedGroup>
   </myGroup>
</configuration>

You can read the value of the new configuration section defined in the preceding example as follows:

Dim config As NameValueCollection =      
   ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection")
Response.Write("The value of key_one is " & Server.HtmlEncode(config("key_one")) & "<br>")
Response.Write("The value of key_two is " & Server.HtmlEncode(config("key_two")) & "<br>")
[C#]
NameValueCollection config = (NameValueCollection)
   ConfigurationSettings.GetConfig("myGroup/nestedGroup/mySection");
Response.Write("The value of key_one is " + Server.HtmlEncode(config["key_one"]) + "<br>");
Response.Write("The value of key_two is " + Server.HtmlEncode(config["key_two"]) + "<br>");

See Also

ASP.NET Configuration | Configuring Applications