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:

<appSettings>
<add key="SomeSetting" value="SomeValue"/>
<add key="SomeOtherSetting" value="SomeOtherValue"/>
</appSettings>

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.

/// <summary>
/// Custom configuration section for social media
/// </summary>
public class SocialMediaConfiguration : ConfigurationSection

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

/// <summary>
/// Gets the twitter account name
/// </summary>
[ConfigurationProperty("twitterAccountName")]
public string TwitterAccountName
{
    get
    {
        return this["twitterAccountName"] as string;
    }
}

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.

<configuration>
<configSections>
  <section name="socialMediaConfiguration" 
type="DBLOG.CustomConfigurationSection.SocialMediaConfiguration, DBLOG.CustomConfigurationSection, Version=1.0.0.0"/>
</configSections>

<socialMediaConfiguration configSource="SocialMedia.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.

<socialMediaConfiguration
twitterAccountName="bergdaniel"
twitterAccountPassword="Secret!"
youTubeAccountName="bergdaniel"
youTubeAccountPassword="Secret!"
/>

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.

SocialMediaConfiguration socialMediaConfig =
    ConfigurationManager.GetSection("socialMediaConfiguration")
        as SocialMediaConfiguration;

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

string twitterAccountName = socialMediaConfig.TwitterAccountName;

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!

blog comments powered by Disqus