How to create and use custom configuration sections in ASP.NET

Posted written by Paul Seal on October 10, 2016 .NET Framework

What is this and when should I be using it?

When developing .NET applications, you may want to have some settings stored in your config file so you can change the behaviour of the application without changing the code.

If you have just a couple of settings to include in the config, you could just use appSettings.

This is fine as long as you don't have lots of settings, if you do, you would be better off putting those settings in a custom configuration section. This post will show you how to do that.

Getting started

  • Create a Class library if you don't have one already
  • Add a folder called Config
  • Inside Config add a class called Section
  • Inside Config add a folder called Elements
  • In your web config file at the root of the site you need to add a new section to the configSections
  <configSections>
    <section name="<mark>CodeShare</mark>" type="CodeShare.Library.Config.Section" />
  </configSections>

The name is the name you will use for this section lower down in the web.config file.

The type is defined by the namespace and class name for your Section.cs file.

  • Add your config section in the web config underneath </appSettings> and inside it, add an element called website with an attribute of domainAddress. This is just for the purposes of this tutorial. You create your own elements later.
  <<mark>CodeShare</mark>>
    <website domainAddress="http://www.codeshare.co.uk"/>
  </<mark>CodeShare</mark>>

Create a class for the element

For each of the elements inside the custom section, you want to create a class for it.

  • On that note, you need to add a new class called Website.cs inside the Elements folder
using System.Configuration;
namespace CodeShare.Library.Config.Elements
{
    public class Website : ConfigurationElement
    {
        [ConfigurationProperty("domainAddress", IsRequired = true)]
        public string domainAddress
        {
            get { return (string)this["domainAddress"]; }
            set { this["domainAddress"] = value; }
        }
    }
}
  • Save the Website.cs Class

Namespaces and references

As you can see, this needs to use the namespace System.Configuration, you need to add a reference to this in your project:

  • Right click on References
  • Add reference
  • Under Assemblies, in Framework, find System.Configuration and make sure it is checked, then click OK.

Create a class for the configuration section

You need to create a class to represent the configuration section, so it will have properties inside it for the elements in the section, i.e. Website

Now go to the Section.cs Class we created and add the following code:

using System.Configuration;

namespace CodeShare.Library.Config
{
    public class Section : ConfigurationSection
    {
        public static Section Get()
        {
            return (Section)ConfigurationManager.GetSection("<mark>CodeShare</mark>");
        }
        [ConfigurationProperty("website", IsRequired = true)]
        public Elements.Website website
        {
            get { return (Elements.Website)this["website"]; }
            set { this["website"] = value; }
        }
    }
}


Using the code in your project

inside your controller class or elsewhere when you might need to access config properties, you can now add this private static variable

private static Library.Config.Section config
{
    get { return Library.Config.Section.Get(); }
}

Or if you just want to use it once in one method you could do this instead

Library.Config.Section config = Library.Config.Section.Get();
string domainAddress = config.website.domainAddress;