Keep web.config tidy using custom configuration sections in ASP.NET


This blog post is about creating a custom configuration section in ASP.NET. This is an alternative to keeping website settings in web.config or appsettings.config.

Perhaps you’ve seen this before in web.config:

Godzilla To you as the developer of the website this might be intiuitive and the right place to store all settings that applies to the website. But for the next developer that comes along this approach might not be the most logical if the website is configuration-heavy.

Sometimes the next developer in line is Japan and the web.config file is Godzilla. Bad combo!

If you’re going to store a number of settings that are logically grouped you might want to consider using a custom configuration section instead of putting everything in in the appSettings-element. In this post I use the ever popular theme of social media to exemplify this approach.

I’m not going to get into exception management because then we’ll have a mile long post. I think you’ll discover where to watch out for a NullReferenceException or two. 🙂

Defining a custom configuration section in ASP.NET

In order two create a custom configuration section in ASP.NET you need to do two things:

  • Create a class that inherits from the abstract class System.Configuration.ConfigurationSection
  • Declare the type it in web.config

You could create a class that exposes a number of properties using ConfigurationManager.AppSettings[“SomeValue”] to retrieve your values. But this would still involve using the appSettings-element for all your values. Let’s avoid this.

Creating the custom configuration section class

We need to inherit our custom configuration section class from System.Configuration.ConfigurationSection.

Next we’re going to define the properties available in this section.

In the same fashion you can create all the properties you require.

What to do in web.config

In order to access the custom configuration section we’ll need to define it in web.config.

Since we do not wish to clutter web.config with our settings we separate the configuration into its own file using the configSource-attribute. In SocialMedia.config we store the following.

Using the custom configuration section

In order to access the properties you’ve stored in the custom configuration section you need to retrieve it with the static ConfigurationManager class.

Now we can retrieve the properties defined in the configuration section.

I suggest wrapping the custom configuration section in a manager-class so you don’t have to use the GetSection-method of the ConfigurationManager-class each and everytime you need to access properties defined in the custom configuration section. An example of a manager class can be found in the source code.

Download the source code

The source code for this is available here. It contains the following:

  • The custom configuration section
  • The custom configuration manager class
  • The config-files

Happy coding!

Recent Posts
  • Exactly what I was looking for. The world is small 😉

    Thank you man!

Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search