How to create custom configuration section in ASP.Net

In this article, I am going to describe how to create your own custom configuration section in Asp.net and how to use it, in a few very simple steps.

Step 1: Create a public class which you want to refer as the class that is holding up the config section, give it some name which shows its stores some kind of setting lets say “UserSettings” and make the class inherited by ConfigurationSection

public class UserSettings : ConfigurationSection


Step 2: Create some properties in this class which have the “ConfigurationProperty” Attribute in this manner

[ConfigurationProperty("MyVariable1", IsRequired = true)]
public string MyVariable1
{
    get { return (string)this["MyVariable1"]; }
    set { this["MyVariable1"] = value; }
}

Lets create three such test properties and then this is how the class looks like

public class UserSettings : ConfigurationSection
{
    [ConfigurationProperty("MyVariable1", IsRequired = true)]
    public string MyVariable1
    {
        get { return (string)this["MyVariable1"]; }
        set { this["MyVariable1"] = value; }
    }

    [ConfigurationProperty("MyVariable2", IsRequired = true)]
    public string MyVariable2
    {
        get { return (string)this["MyVariable2"]; }
        set { this["MyVariable2"] = value; }
    }

    [ConfigurationProperty("MyVariable3", IsRequired = true)]
    public string MyVariable3
    {
        get { return (string)this["MyVariable3"]; }
        set { this["MyVariable3"] = value; }
    }
}


Step 3: Open the web.config file and define a Config Section under the tag <Configuration> like this,

  <configSections>
    <section name="UserSettings" type="Example.Web.Utilities.UserSettings, Example.Web, 
             Version=1.0.0.0, Culture=neutral" 
             allowLocation="true" allowDefinition="Everywhere" />
  </configSections>

Now this is the information about the library that gets created when you “Build”

image_thumb9

next step, we tell the config section what “UserSettings” is,

  <UserSettings MyVariable1="Value1"
                MyVariable2="Value2"
                MyVariable3="Value3" />

and altogether it looks like this

<configuration>
  <configSections>
    <section name="UserSettings" type="Example.Web.Utilities.UserSettings, Example.Web, 
             Version=1.0.0.0, Culture=neutral" 
             allowLocation="true" allowDefinition="Everywhere" />
  </configSections>

  <UserSettings MyVariable1="Value1"
                MyVariable2="Value2"
                MyVariable3="Value3" />
  
  <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
</configuration>

Step 4: We go to the code behind of the default page of project and fetch the settings

public class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userSettings = ConfigurationManager.GetSection("UserSettings") as UserSettings;
            if(userSettings == null) return;

            Response.Write(
                "MyVariable1 = " + userSettings.MyVariable1 + "</br>" +
                "MyVariable2 = " + userSettings.MyVariable2 + "</br>" +
                "MyVariable3 = " + userSettings.MyVariable3 + "</br>");
        }
    }

Next, I go run the application and get this

image_thumb10

I have attached the sample project that we created through this post.

Posted by: Zeeshan

Categories: ASP.NET, C#

Tags: , , ,

Comments (2) -

nittu United States

March 16. 2012 20:01

nittu
There is better way of managing Configuration in .NET using Cinchoo Framework.

Just define a configuration class as below

  #region NameSpaces

  using System;
  using Cinchoo.Core.Configuration;

  #endregion NameSpaces

  [ChoConfigurationSection("sample")]
  public class SampleConfigSection : ChoConfigurableObject
  {
    #region Instance Data Members (Public)

    [ChoPropertyInfo("name", DefaultValue="Mark")]
    public string Name;

    [ChoPropertyInfo("message", DefaultValue="Hello World!")]
    public string Message;

    #endregion
  }

Then instantiate it and use it as below,

    class Program
    {
      static void Main(string[] args)
      {
        SampleConfigSection sampleConfigSection = new SampleConfigSection();
        Console.WriteLine(sampleConfigSection.ToString());
    
        //Shutdown the framework to stop the background services...otherwise the application will not terminate
        ChoFramework.Shutdown();
      }
    }

Corresponding entries will be created in applicationexename.xml file as below

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="sample" type="Cinchoo.Core.Configuration.Handlers.ChoNameValueSectionHandler, Cinchoo.Core" />
      </configSections>
      <cinchoo>
      <sample>
        <add key="name" value="Mark" />
        <add key="message" value="Hello World!" />
      </sample>
    </configuration>

For more information, please visit http://www.cinchoo.com

zeeshan India

March 16. 2012 23:25

zeeshan
Okay, but when we do that, we are back to where we started, The whole idea behind this was NOT to use a Key Value pair because they become quite difficult to maintain as the list of your settings grows.
Check out the first paragraph of this post below, it will give a better understanding.
haacked.com/.../...n-sections-in-3-easy-steps.aspx

Add comment

  Select your country

biuquote
  • Comment
  • Preview
Loading