Web.config consists of different configuration sections which allow the developer to configure the application specific to their needs. ASP.NET 2.0 also allows you to create your own custom sections. In this article we will learn how to create a custom Web.config section.

Introduction:

Web.config consists of different configuration sections which allow the developer to configure the application specific to their needs. ASP.NET 2.0 also allows you to create your own custom sections. In this article we will learn how to create a custom Web.config section.

Inheriting a Class from ConfigurationSection:

In this article I will create a simple config section that will enable the developer to write the name of the data access class.  The first thing that you need to do is to create a class which inherits from the ConfigurationSection class. Take a look at the code below:

public class MyDataAccessSection : ConfigurationSection

    {

 
The data access section will contain a single property called “dataAccessClassName” which will contain the name of the data access class. Below you can see the complete code for the MyDataAccessSection class.

using System;

using System.Data;

using System.Configuration;

using System.Web;

 

namespace MyClassLibrary

{

    public class MyDataAccessSection : ConfigurationSection

    {

        private static ConfigurationProperty propDataAccessClass = null;

 

        public MyDataAccessSection()

        {

            MyDataAccessSection.propDataAccessClass = new ConfigurationProperty(

                "dataAccessClassName", typeof(string), "MyDataAccessClass", ConfigurationPropertyOptions.IsRequired);

 

        }

 

        [ConfigurationProperty("dataAccessClassName")]

        public string DataAccessClassName

        {

            get { return (string)base[MyDataAccessSection.propDataAccessClass]; }

            set { base[MyDataAccessSection.propDataAccessClass] =  value; }

        }

 

    }

}

 

 
Let’s take a look at the code piece by piece. In the constructor of the class we define the ConfigurationProperty. The name of the ConfigurationProperty is dataAccessClassName which is of type string with a default value “MyDataAccessClass”. The
ConfigurationPropertyOptions is an enumeration and is set to IsRequired which means that the property is required for the configuration section. Next, we define the property DataAccessClassName which is decorated with the [ConfigurationProperty("dataAccessClassName")] attribute. The attribute specified that the property is a configuration property. 

Settings in Web.config:

Before you start playing with your config section you need to make one last setting in the web.config file. You need to tell the web.config section that you will be using a custom section. Take a look at the code below which registers the custom config section.

      <configSections>

            <section name="dataAccessSection" type="MyClassLibrary.MyDataAccessSection,MyClassLibrary"/>

      </configSections>

 The name attribute sets the name for the data access section and the type attribute denotes the type of the section. MyClassLibrary.MyDataAccessSection is the name of the class and MyClassLibrary is the name of the assembly.

Declaring the Custom Section:

Declaring a custom section is very simple. All you need is the name of the section and the properties associated with the section.
 

        <dataAccessSection dataAccessClassName="GridViewGuyDataAccessClass">  

   

  </dataAccessSection>

 

Accessing the Custom Section Information:

Accessing the custom section information is pretty simple. Take a look at the code below which accesses the custom section.

            // Get the custom section

      MyDataAccessSection section = (MyDataAccessSection) ConfigurationManager.GetSection("dataAccessSection");

 

      // display the custom section

      Response.Write(section.DataAccessClassName);

 

Conclusion:

In this article we learned that how we can use simple custom sections in web.config. You can also define the complete configuration section which includes other elements and more attributes.