Creating Custom Configuration Sections by inheriting a new class from ConfigurationSection class : New Section « Configuration « ASP.NET Tutorial






File: App_Code\DesignSection.cs

using System;
using System.Configuration;
using System.Drawing;

namespace MyNamespace
{
    public class DesignSection : ConfigurationSection
    {
        [ConfigurationProperty("backcolor", DefaultValue = "lightblue", IsRequired = true)]
        public Color BackColor
        {            get { return (Color)this["backcolor"]; }
            set { this["backcolor"] = value; }
        }

        [ConfigurationProperty("styleSheetUrl", DefaultValue = "~/styles/style.css", IsRequired = true)]
        [RegexStringValidator(".css$")]
        public string StyleSheetUrl
        {
            get { return (string)this["styleSheetUrl"]; }
            set { this["styleSheetUrl"] = value; }
        }

        public DesignSection(Color backcolor, string styleSheetUrl)
        {
            this.BackColor = backcolor;
            this.StyleSheetUrl = styleSheetUrl;
        }

        public DesignSection()
        {
        }
    }
}

Register it in a configuration file. 

File: Web.config

<configuration>
  <configSections>
    <sectionGroup name="system.web">
    <section
        name="design"
        type="MyNamespace.DesignSection"
        allowLocation="true"
        allowDefinition="Everywhere"/>
    </sectionGroup>
  </configSections>
  <system.web>
    <design
      backcolor="red"
      styleSheetUrl="~/styles/style.css"/>
  </system.web>
</configuration>








16.25.New Section
16.25.1.Creating Custom Configuration Sections by inheriting a new class from ConfigurationSection class
16.25.2.Using the custom configuration section to modify the page style and background color.
16.25.3.Creating a Configuration Element Collection