Many times we come across situations when we need to define our own settings( Rather than using AppSettings key/value).
This article is just a try to make you understood how we can achieve such functionality.
Introduction
In this article I will describe the step by step procedure how you can create your custom Section of Web.config / app.config and later on use it from your application.
Creating Custom Config Section
Let us suppose that we need to create following custom configuration section in web.config file.
<MyCustomSection>
<MySettings>
<add name = "Suraj"city="Bombay" email="xyzxyz1@gmail.com"></add>
<add name = "Amit"city="Delhi"email="xyzxyz2@gmail.com"></add>
</ MySettings>
</ MyCustomSection>
We will create this custom section using following steps.
Step 1
Create a web application and write the above section code in web.config file and run the application. It will display below error.
It is because we have not defined handler for this custom section. So, first task is to create custom section handler class.
To do this add a class file (.cs) in project and Name it as “MyCustomSectionHandler.cs”. Inherit this class with interface IConfigurationSectionHandler and provide implementation for the Create method provided by the interface.
Create method takes three arguments:
1.
object parent
This contains configuration settings for parent configuration section.
2.
object configContext
This is nothing but the reference to HttpConfigurationContext class.
3.
System.Xml.XmlNode section
This parameter contains the required settings.
From above it is clear that 3rd parameter is important for reading settings which we are interested in.
Below is code for MyCustomSectionHandler.cs file where I have implemented Create method to return settings.
using System;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Xml;
namespace CustomSectionProject
{
public class MyCustomSectionHandler: IConfigurationSectionHandler
{
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
List<MySettings> mySettings = newList<MySettings>();
XmlNodeList nodelist = section.SelectNodes("MySettings//add");
if (nodelist != null)
foreach (XmlNode node in nodelist)
{
mySettings.Add(
new MySettings(
node.Attributes["name"].InnerText,
node.Attributes["city"].InnerText,
node.Attributes["email"].InnerText));
}
return mySettings;
}
}
/// <summary>
/// Class for holding our custom settings
/// </summary>
public class MySettings
{
private string name = string.Empty;
private string city = string.Empty;
private string email = string.Empty;
public MySettings(string name, string city, string email)
{
this.name = name;
this.city = city;
this.email = email;
}
public string Name
{
get
{
return name;
}
}
public string City
{
get
{
return city;
}
}
public string Email
{
get
{
return email;
}
}
}
}
Step 2 : Registering handler in web.config file
step 2 is registering handler in web.config file. We register handler in web.config as below
< configuration >
< configSections >
< section name = "MyCustomSection" type = "CustomSectionProject.MyCustomSectionHandler,CustomSectionProject " />
</ configSections >
< MyCustomSection >
< MySettings >
< add name = "Suraj"city="Bombay"email="xyzxyz1@gmail.com"></add>
<add</MySettings>
</MyCustomSection>
< appSettings />
<connectionStrings />
In <configSections> add section tag. Which asks name and type. Name is the name of section. Type is fully qualified name of handler class and with comma, name of assembly .
Here we done with all required configuration and handler implementation. Next step is retrieving our settings.
Step 3 : Retrieving settings
With below code you can get the settings defined.write below code in page load of any page say “default.aspx”.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
namespace CustomSectionProject
{
publicpartialclass_Default : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
List<MySettings> mySettings = (List<MySettings>)ConfigurationManager.GetSection("MyCustomSection");
foreach (var item in mySettings)
{
Response.Write("<b>Name</b> :"+(item asMySettings).Name);
Response.Write(" <b>City</b> :"+(item asMySettings).City);
Response.Write(" <b>Email </b>:" +(item asMySettings).Email);
Response.Write("<Br>");
}
}
}
}
Now save and run the application you will get the output as below.
Conclusion
This way you can easily create your custom section in Web.config and directly access it from your application.
Reference:
http://msdn.microsoft.com/en-us/library/ms228056.aspx