Click here to Skip to main content
15,881,204 members
Articles / Web Development / ASP.NET

ASP.NET Custom Web Configuration Section

Rate me:
Please Sign up or sign in to vote.
3.14/5 (4 votes)
18 Jan 2009CPOL4 min read 43.7K   501   28   1
Learn how to define and use your own web.config sections for code libraries.

Introduction

The web.config can be a very useful tool in web development. It allows you to place settings that can be accessed by your website and changed without having to re-deploy your code. But, what happens when you want to build a code library and have the web.config settings for the library? Sure, you can use the standard <appSettings> section, but there is another way. You can create a custom web.config section.

Background

I believe it is important to make everything (within reason) reusable. Anything you would use on more than one project should be placed into a reusable code library. Anything you want to Unit Test or run through FXCop should be in its own class library. Also, I believe that these code libraries should have a way to change their settings to make them usable in multiple scenarios.

But, what if you have a library that could be used in both ASP.NET websites and Windows applications? If you use System.Configuration.ConfigurationManager.AppSettings["Key"], it will actually refer to the same key in either the web.config or app.config. That is a very simple and easy way to do it, but this article is about making a custom web configuration. I have not tested it, but this custom section may or may not work in the application configuration. This article will focus on the use of the custom configuration in a web environment.

Define the Attributes

The first step is to determine what settings you want to expose. Once you are sure you know what you want to expose, it is time to create the ConfigurationSection definition class. In the case of my caching library, I called it CacheConfiguration. Note that it must extend from System.Configuration.ConfigurationSection.

C#
public class CacheConfiguration : ConfigurationSection

Next is defining the attributes. For this class, I am exposing integer values corresponding to lengths of time in sections to associate with a cache priority. Shown below is a sample of one of the attributes:

C#
// Number Property
[ConfigurationProperty("highPriority", DefaultValue = 60, IsRequired = false)]
[IntegerValidator(MinValue = 0)]
public int HighPriority
{
    get
    { return (int)this["highPriority"]; }
}

ConfigurationProperty is a definition of the attribute, including the name, default value, and whether or not the attribute is required. IntegerValidator allows me to define the minimum value, which is zero in this case.

Once thing you should be aware of is that the attribute name (highPriority) is case sensitive when placed in the web.config.

Access the Attributes

To make things easier on myself, I create a Configuration class that acts as a wrapper around the configuration section. This exposes the properties more easily with a simple reference to a static class, rather than having to create an instance of the config section each time it needs to be accessed.

C#
public static class Configuration
{
    public static CacheConfiguration CacheConfiguration
    {
        get
        {
            CacheConfiguration config = (CacheConfiguration)
              System.Configuration.ConfigurationManager.GetSection("olympus/cache");

            return config;
        }
    }
}

Did you notice the node declaration? The string parameter "olympus/cache" tells GetSection what node my attributes are in. If I wanted to place my config inside <system.web>, I could change it to "system.web/cache". I prefer to have my section in its own wrapper.

Web.Config - Declare the Section

XML
<configuration>
    <configSections>
    <sectionGroup name="olympus">
      <section name="cache" type="Olympus.Caching.CacheConfiguration" 
        allowLocation="true" allowDefinition="Everywhere"/>
    </sectionGroup>
</configSections>
...

Below the <configSections> block, inside <configuration> and not in <system.web>, place the attributes with their values.

XML
<olympus>
    <cache highPriority="360"  />
</olympus>

Element Collections

So, now that you have a basic understanding of custom configurations, let's move on to something a bit more advanced. What if you wanted a collection of elements, such as a collection of cookies used by your site? Well, with an additional class and a few modifications, you can make a web.config section that looks like this:

XML
<olympus>
    <cookie>
      <cookies>
        <add name="My_Cookie_1" enableEncryption="true" timeout="4200" />
        <add name="My_Cookie_2" enableEncryption="true" timeout="4200" />
      </cookies>
    </cookie>
</olympus>

All we need to do is add a ConfigurationElementCollection class to wrap the ConfigurationSection into a collection. I have a class already built that handles cookies encrypted with Rijndael, Triple DES, or DES (user's choice), but encryption and cookies are topics for another day.

C#
public class CookieConfig : ConfigurationSection

CookieConfig is the class that contains the attributes. This corresponds identically to CacheConfiguration.

C#
public class CookieCollectionConfiguration : ConfigurationElementCollection
{

    public CookieConfig this[int index]
    {
        get
        {
            return base.BaseGet(index) as CookieConfig;
        }
        set
        {
            if (base.BaseGet(index) != null)
            {
                base.BaseRemoveAt(index);
            }
            this.BaseAdd(index, value);
        }
    }

    public CookieConfig this[string name]
    {
        get
        {
            return base.BaseGet(name) as CookieConfig;
        }
        set
        {
            int index = -1;
            if (base.BaseGet(name) != null)
            {
                index = base.BaseIndexOf(base.BaseGet(name));
                base.BaseRemove(name);
            }

            if (index == -1)
            {
                this.BaseAdd(value);
            }
            else
            {
                this.BaseAdd(index, value);
            }
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new CookieConfig();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((CookieConfig)element).Name;
    } 
}

So, there you have it, custom web.configuration sections for your code libraries. I recommend creating an example of the web.config section with all fields defined and including the file in the code library project. This will make it easy to figure out how to use your custom configuration when adding the library to a new web application. One of the issues with a custom section is that you only have the documentation you create, and there is no intellisense support.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Other
United States United States
I have been a Techincal Consultant in Omaha, NE specializing in .NET technology since 2007. My latest projects include hyperspectral imaging, Learning Management Systems, Content Management Systems, Online StoreFronts, medical web applications, and other websites. Before becoming a consultant I spent 2 years developing ASP.NET websites for a large market research company. My favorite programming language is C#.

In my free time I enjoy studying the Japanese culture and language, practicing a Japanese sword art called Shinkendo, and spending time with my family.

Comments and Discussions

 
GeneralMy vote of 1 Pin
codeguruj23-Jan-09 6:24
codeguruj23-Jan-09 6:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.