The web.config doesn't look like much, but it is your best friend when it comes to configuring your application to perform a certain way, display a certain way and many other things that can make your life a whole lot easier.
In my opinion, one of the most important things about the web.config, that most of us seem to neglect, is the ability to add any value you feel could change at any time on the site that normally would require a recompile and a code push. Some people feel that filling your web.config up with keys in your area is messy and can become a pain to manage. Well, they are partially correct. The appSettings section can be useful for values that are used frequently in different areas of your site. However, the draw back to that is that they are simply key/value pairs, meaning that you are limited to a key and a value and nothing more. Say for instance you want to configure how an image is displayed by specifying the width, height, alternate text and the image url. Using the standard key/value pair dictionary provided for you in the web.config just won't give you what you need to make this happen.
An easy solution to this is to create your own section handler to handle any sort of XML you would like to add to the web.config with the added functionality for easy readability and portability.
Below is a snippet of code I have written so that I can add pieces of configuration to my web.config with as much detail as I need to store.
using System;
using System.Data;
using System.Xml;
namespace AspAdvice.Configuration
{
public class AspAdviceConfigSectionHandler : System.Configuration.IConfigurationSectionHandler
{
public AspAdviceConfigSectionHandler()
{
}
#region IConfigurationSectionHandler Members
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
return new AspAdviceConfig(section.ChildNodes);
}
#endregion
}
public class AspAdviceConfig
{
private System.Xml.XmlNodeList nodesHere;
internal AspAdviceConfig (System.Xml.XmlNodeList configNodes)
{
nodesHere = configNodes;
}
public XmlNodeList GetXmlNodeList()
{
return nodesHere;
}
public DataTable ToDataTable()
{
DataTable myTable = new DataTable();
if (nodesHere.Count > 0)
{
for (int x=0;x {
myTable.Columns.Add(new DataColumn(nodesHere.Item(0).Attributes[x].Name));
}
for (int i=0;i {
DataRow myRow = myTable.NewRow();
for (int e=0;e {
myRow[nodesHere.Item(i).Attributes[e].Name] = nodesHere.Item(i).Attributes[e].InnerText;
}
myTable.Rows.Add(myRow);
myRow = null;
}
return myTable;
}
else
{
throw new System.Configuration.ConfigurationException("There are no elements beneath the specified config section");
return null;
}
}
}
}
The code above simply takes the configuration section you specify and runs it through the method you specify, such as ToDataTable() or GetXmlNodeList() and returns it to you so
that you can easily navigate through it and grab your values. So now, we can take something such as:
![”Hello”]()
![”Dog”]()
![“Dog“]()
And easily navigate through it now using your custom configuration section handler we have written. Now, keep in mind that beneath your section, you will need to add the following tag:
This tag tells .NET to use the AspAdvice.Configuration.AspAdviceConfigSectionHandler class to read this config section and that it is located in the ASPAdvice.Library assembly.
To implement this, you would simple add this snippet of code:
DataTable images = ((AspAdvice.Configuration.AspAdviceConfig)System.Configuration.ConfigurationSettings.GetConfig(“Images")).ToDataTable();
or
XmlNodeList imageList = ((AspAdvice.Configuration.AspAdviceConfig)System.Configuration.ConfigurationSettings.GetConfig(myConfig["Domain"].ToUpper() +
"/Sponsors")).GetXmlNodeList();
Hope this was helpful. I would love to hear that if you use this, how it helped you. Keep posted for upcoming snippets and tutorials.