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

Creating Custom Configuration Sections

Introduction

ASP.NET allows you to store your own configuration settings in the <appSettings> section of web.config file. This section allows you to store settings as key-value pairs. This works well if you have few key-value pairs. However, if you have many such configuration settings then it becomes bit difficult to categorically store the values. Good part is that ASP.NET allows you to define your own sections to store such settings. In this article we will see how to create your own configuration sections and access them in your code.

Section Handlers

The web.config file consists of several sections and each such section is supposed to be processed by a section handler. A section handler is nothing but a class that implements IConfigurationSectionHandler interface. In this article we will not go into the details of this interface but we will see how to use one built-in section handler. You can specify the section handler for a particular section in the web.config file itself.

Specifying Section Handler

Let's assume that you want to create a custom configuration section like this:

<sitesettings>
<common>
<emails>
<add key="adminmail" value="administrator@somedomain.com"/>
<add key="salesmail" value="sales@somedomain.com"/>
<add key="supportmail" value="support@somedomain.com"/>
<add key="admail" value="advertising@somedomain.com"/>
</emails>
</common>
</sitesettings>

Here, we are storing the configuration settings as key-value pairs but they are nested inside other tags.

Now that you know how your section is going to be, let us put markup in web.config that will indicate how this section will be handled. .NET comes with a built-in section handler called NameValueSectionHandler that resides in System.Configuration namespace. This handler is responsible to handle sections that store settings as key-value pairs (such as <appSettings>).  We will be using this section handler for our section.

<configuration>
<configSections>
<sectionGroup name="sitesettings">
<sectionGroup name="common">
<section name="emails" type=
"System.Configuration.NameValueSectionHandler,System"/>
</sectionGroup>
</sectionGroup>
</configSections>
...
...
...

Reading section values in your code

Once you put section handler and sections in your web.config you can access the values in your code as follows:

NameValueCollection nvc = (NameValueCollection)
ConfigurationSettings.GetConfig("sitesettings/common/emails");
Label1.Text = nvc["salesmail"];
Label2.Text = nvc["supportmail"];
Label3.Text = nvc["adminmail"];
...
...

Summary

In this article we saw how to create your custom web.config sections. Web.config sections are handled by classes implementing IConfigurationSectionHandler interface. We used NameValueSectionHandler class that handles <appSettings> section of web.config. In the next article we will see how to create our own class that implements IConfigurationSectionHandler and process our custom sections.

 

 

 




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 : 13 Jun 2004



Tags : ASP.NET Components Configuration