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”

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

I have attached the sample project that we created through this post.
ConfigSectionExample.zip (6.58 kb)
7869a8e1-f0ff-4685-8489-9c630a7ad8e3|0|.0
Categories:
ASP.NET, C#
28. January 2012
Tags:
ASP.Net, Config-Section, Web.Config, C#