Mega Code Archive

 
Categories / ASP.Net Tutorial / Configuration
 

Creating Custom Configuration Sections by inheriting a new class from ConfigurationSection class

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>