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="CodeShare" 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.
  <CodeShare>
    <website domainAddress="http://www.codeshare.co.uk"/>
  </CodeShare>

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("CodeShare");
        }
        [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;

With this you can now add your own custom configuration section to your web.config file and access it in code.

Share with others

If you found this helpful, please share it with others and I would love to hear from you in the comments.

If you'd like to improve your .NET skills, have a look at the courses on Pluralsight

Watch this livecoding.tv video to see me implementing this on a site.

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.
uSkinned

  • More than a theme for Umbraco CMS, take full control of your content and design with a feature-rich, award-nominated & content editor focused website platform.
UmbHost

  • Affordable, Geo-Redundant, Umbraco hosting which gives back to the community by sponsoring an Umbraco Open Source Developer with each hosting package sold.