Home > ASP.NET Applications > ASP.NET Configuration > Creating New Configuration Sections
ASP.NET Web Applications ASP.NET Configuration
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
public Object Create ( Object parent, Object input, XmlNode node );
}
}
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# |
VB |
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 then read the value of the new configuration section defined in the previous example, as follows:
NameValueCollection config = ( NameValueCollection )
ConfigurationSettings.GetConfig ( "mygroup/nestedgroup/mysection" );
Response.Write ( "The value of key_one is " + config [ "key_one" ] + "<br>" );
Response.Write ( "The value of key_two is " + config [ "key_two" ] + "<br>" );
Dim config As NameValueCollection =
ConfigurationSettings.GetConfig ( "mygroup/nestedgroup/mysection" )
Response.Write ( "The value of key_one is " & config ( "key_one" ) & "<br>" )
Response.Write ( "The value of key_two is " & config ( "key_two" ) & "<br>" ) |
|
C# |
VB |
Accessing Configuration Settings ASP.NET Configuration Sections