Learn ASP.NET MVC, ASP.NET Core, and Design Patterns through our online training programs. Courses conducted by Bipin Joshi on weekends. Read more details here.

<%@ Page %>

Creating Custom Configuration Section Handlers

Introduction

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.
	<DBSettings>
		<DatabaseName>aaa</DatabaseName>
		<FileStore>bbb</FileStore>
		<ImageStore>ccc</ImageStore>
	</DBSettings>
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 
    System.Configuration.IConfigurationSectionHandler.Create

        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.
	<configSections>
		<sectionGroup name="DBSettings">
			<section name="DatabaseName"  
			type="CustomConfigSection.Class1,
			CustomConfigSection" />
			<section name="FileStore" 
			type="CustomConfigSection.Class1,
			CustomConfigSection" />
			<section name="ImageStore" 
			type="CustomConfigSection.Class1,
			CustomConfigSection" />
		</sectionGroup>
	</configSections>
Note that <section> tag that specifies section name and class that processes it.

Testing your section handler

Add following settings in web.config file
	<DBSettings>
		<DatabaseName>WINSQL</DatabaseName>
		<FileStore>C:\Files</FileStore>
		<ImageStore>D:\Images</ImageStore>
	</DBSettings>
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 = 
        ConfigurationSettings.
        GetConfig("DBSettings/DatabaseName")
        Dim s1 As String = 
        ConfigurationSettings.
        GetConfig("DBSettings/FileStore")
        Dim s2 As String = 
        ConfigurationSettings.
        GetConfig("DBSettings/ImageStore")
        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.

summary

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 a software consultant, trainer, author and a yogi having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, AngularJS, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Ajapa Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 07 Sep 2002



Tags : ASP.NET Configuration