We can create our own configuration section in the Web.Config file and can store any value we want to.
Below is an example for creating a custom configuration section.Our custom configuration class will look like the following which contains two properties that is SiteName and ForeColor.
using System; using System.Configuration; using System.Web; using System.Drawing; namespace MyNamespace { public class MyCustomConfiguration : ConfigurationSection { [ConfigurationProperty("SiteName", DefaultValue = "WelCome To My Site", IsRequired = true)] public string SiteName { get { return (string)this["SiteName"]; } set { this["SiteName"] = value; } } [ConfigurationProperty("ForeColor", DefaultValue = "red", IsRequired = true)] public Color ForeColor { get { return (Color)this["ForeColor"]; } set { this["ForeColor"] = value; } } }
and our configuration file will contain the below lines where “MyConfigSectionGroup” is the name of the custom configuration sectiongroup and “customSection” is the name of the custom configuration section.
After the custom configuration section is registered,we can retrieve and modify the custom section by using WebConfigurationManager class.
Below is the code for retriving the custom configuration section in a page.
protected void Page_Load(object sender, EventArgs e)
{
MyCustomConfiguration section = (MyCustomConfiguration)WebConfigurationManager.
GetWebApplicationSection(“MyConfigSectionGroup/customSection”);
Label1.Text = section.SiteName;
Label1.ForeColor = section.ForeColor;