|
The web.config file in ASP.NET is the central location for your web applications configuration. It contains settings such as authentication,
handler settings, compilation settings, globalization settings, tracing and error settings, etc... But what happens when this is not enough? Or
you want to add you own settings into the web.config file. This tutorial will explain how it's done.
To create your own custom configuration handler, it will require two parts: writing some code, and editing your web.config file.
The code
Here we have a small C# file with code to create a new handler that will be used in the web.config file.
using System;
using System.Collections;
using System.Xml;
using System.Configuration;
using System.Web.Configuration;
namespace Devhood {
internal class PageStyleHandler:IConfigurationSectionHandler {
public virtual object Create(Object parent, Object context, XmlNode node) {
PageStyle config = new PageStyle((PageStyle)parent);
config.LoadValuesFromConfigurationXml(node);
return config;
}
}
public class PageStyle {
string _backColour;
internal PageStyle(PageStyle parent) {
if (parent != null)
_backColour = parent._backColour;
}
internal void LoadValuesFromConfigurationXml(XmlNode node) {
XmlAttributeCollection attribCol = node.Attributes;
_backColour = attribCol["backColour"].Value;
}
public string BackColour {
get {
return _backColour;
}
}
}
}
|
There are two classes here, the PageStyleHandler class which implements the IConfigurationSectionHandler, and the PageStyle class which is used
to store and retrieve the configuration data.
The PageStyleHandler contains the Create method. It is used to create and instance of the PageStyle class to pass the data from the web.config
file.
The PageStyle class will accept an XML node which comes from the web.config file, it reads the attribute from the XML node and it will save the
data for future retrieval by the BackColour property.
The web.config file
To add your custom handler to the web.config file for this application, it requires simply editing the web.config file so that it will accept
your new handler. Your new web.config file will look like this:
<configuration>
<configSections>
<sectionGroup name="devhood">
<section name="pageStyle" type="Devhood.PageStyleHandler, PageStyle" />
</sectionGroup>
</configSections>
<devhood>
<pageStyle backColour="navy" />
</devhood>
</configuration>
|
Note that this example will only apply to the web application that this file resides in. If you would like this new handler to apply to all web
applications on this server, the <sectionGroup> tag can be moved to the machine.config.
An example usage in an ASPX page
Here is an example of our new custom handler in action:
|
<%@ Import Namespace="Devhood" %> |
<html> <head> <title>ASP.NET Configuration</title> <script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e) { PageStyle _pageStyle; _pageStyle = (PageStyle) Context.GetConfig("devhood/pageStyle"); bodyTag.Attributes["bgcolor"] = _pageStyle.BackColour; } </script> </head> <body id="bodyTag" runat="server"> <table bgcolor="white" align="center" width="400"><tr><td> <p align="center"> <font size=+2>This background is Navy!</font> </p> <p align="center"> <font size=+2>Created for <a href="https://devhood.com">devhood.com</a></font> </p> </td></tr></table> </body> </html>
|
The custom configuration handler in ASP.NET is a useful addition for creating really flexible web pages. Usage for custom configuration handlers
can be for: allowing the web applications style to be defined in one web.config file, saving information that is commonly used (ie. DSN), and
whatever else you can think of.
That's it for this tutorial. Hope this is helpful for everyone. Feel free to find me in the Devhood ASP.NET Message Forum if you have any questions, or just
send me a private message.
Tips
- Don't forget the import namespaces directives at the beginning.
- configuration files (web.config and machine.config) are case sensitive.
- ASP.NET configuration settings uses camel-casing (the first letter of the first word is always lower-case and the first letter of subsequent
words are upper-case). We recommend that you stick to this type of casing for consistency.
Copyright © 2001 Andrew Ma.
|
|