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.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

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.

Related Posts

How to include scripts from partial views in MVC and Umbraco

This post will show you how you can include scripts from inside an MVC partial view rather than havi…

Read Post

How to fix the error "Authentication failed because the remote party has closed the transport stream"

This post will show you how to fix the error "Authentication failed because the remote party has clo…

Read Post

How to solve the error Could not load file or assembly System.Security.Principal.Windows

This post help you solve the error System.IO.FileNotFoundException Could not load file or assembly S…

Read Post

How to solve the error "Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0 ..."

This post will show you how to solve the error "Could not load file or assembly 'Microsoft.CodeDom.P…

Read Post