<%@ Page %>

Creating Custom Configuration Section Handlers


Most of the applications require some kind configuration. The configuration involves database connection strings, file storage paths and so on. ASP.NET provides a neat way to store your configuration settings via wb.config file. You typically use the <appSettings> tag to store your custom application settings. However, some times you may need to store some complex settings in which case <appSettings> section may prove to be too elementary. ASP.NET allows you to create your own configuration sections. In this article we will see how to create your own configuration section handlers.

IConfigurationSectionHandler Interface

The configuration files contain various sections (e.g. <appSettings>, <authentication> ). Each such section is processed by a class that implements System.Configuration.IConfigurationSectionHandler interface. This interface looks like this:
Public Interface IConfigurationSectionHandler
 Function Create(parent As Object, input As Object, _
                 node As XmlNode) As Object
End Interface
Out of the three parameters the last one i.e. section is of our interest. It represents the XML node that contains the configuration information to be handled. It provides access to the XML contents of the configuration section.

Implementing IConfigurationSectionHandler

Now that we know that in order to create custom configuration section handlers we need to implement above interface let us decide our configuration section format first.
Here, we want to store some settings in format as shown above. Now we will create our class to handle these sections. Create a ASP.NET web application in VS.NET and add a class to it named MyConfigHandler. Put following code in it.
Imports System.Xml

Public Class MyConfigHandler
    Implements IConfigurationSectionHandler

    Public Function Create(ByVal parent As Object,
    ByVal configContext As Object,
    ByVal section As System.Xml.XmlNode)
    As Object Implements 

        Select Case section.Name
            Case "DatabaseName"
                Return section.ChildNodes(0).Value
            Case "FileStore"
                Return section.ChildNodes(0).Value
            Case "ImageStore"
                Return section.ChildNodes(0).Value
            Case Else
        End Select
    End Function
End Class
In our example, we are simply returning the values of the text child nodes. You can do any XML operations here.

Adding your section in Web.config

Now, it's time to tell ASP.NET which section will be processed by your class. This is done by adding following markup in web.config file.
		<sectionGroup name="DBSettings">
			<section name="DatabaseName"  
			CustomConfigSection" />
			<section name="FileStore" 
			CustomConfigSection" />
			<section name="ImageStore" 
			CustomConfigSection" />
Note that <section> tag that specifies section name and class that processes it.

Testing your section handler

Add following settings in web.config file
Note how this structure matches with that provided in <configSections> tag. Now add a web form to the project and write following code to test our settings.
    Private Sub Page_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load
        Dim s0 As String = 
        Dim s1 As String = 
        Dim s2 As String = 
        Response.Write(s0 & "
") Response.Write(s1 & "
") Response.Write(s2 & "
") End Sub
Notice how we specified the section whose value is to be extracted by XPath kind of mechanism. When you will run the web form you should see the values you placed in web.config under your section.


ASP.NET provides neat way to place your configuration settings in web.config. Some times when you need to store some complex settings you can create your own configuration sections. In order to process these sections you need to create a class that implements IConfigurationSectionHandler interface.

Bipin Joshi is an independent software consultant, trainer, author, and meditation teacher. He has been programming, meditating, and teaching for 25+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 07 September 2002

Tags : ASP.NET Configuration