Have a question? ask www.kenlet.com
Home  |  FAQ  |  About  |  Contact  |  View Source   
 
SEARCH:
 
BROWSE:
    My Hood
Edit My Info
View Events
Read Tutorials
Training Modules
View Presentations
Download Tools
Scan News
Get Jobs
Message Forums
School Forums
Member Directory
   
CONTRIBUTE:
    Sign me up!
Post an Event
Submit Tutorials
Upload Tools
Link News
Post Jobs
   
   
Home >  Tutorials >  General ASP.NET >  Adding Customized Tags in web.config
Add to MyHood
   Adding Customized Tags in web.config   [ printer friendly ]
Stats
  Rating: 4.64 out of 5 by 36 users
  Submitted: 12/09/01
Andrew Ma ()

 
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.

Return to Browsing Tutorials

Email this Tutorial to a Friend

Rate this Content:  
low quality  1 2 3 4 5  high quality

Reader's Comments Post a Comment
 
Great tutorial. Easy to follow. Very useful. Maybe you can write one on web services using notepad and .NET command line tools.
-- Jason Perron, December 10, 2001
 
Nice overview of custom tags and how to use them.....
-- Aaron Roberts, December 11, 2001
 
very useful info...
-- Vijay Venkatachalam, December 11, 2001
 
Good Job
-- Smith Green, December 11, 2001
 
This is a well written tutorial. Thanks Andrew. Hope to see more from you.
-- Alexis Kwong, December 11, 2001
 
Very straightforward and easy to understand.
-- Chris Roberts, December 12, 2001
 
Good tutorial with good examples.
-- Reid Jonasson, December 14, 2001
 
Don't set your section name="config". Stupid thing gives you an error and doesn't tell you that it doesn't like the name set to config......
-- Andrew Ma, December 14, 2001
 
Very interesting tutorial and quite useful indeed! Another great tutorial by Andrew!
-- Robert Wlodarczyk, December 22, 2001
 
This is a good tutorial. It may be a good idea to do a brief description at the beginning of why exactly the web.config file is very important and needs to be used.
-- Alvin Leung, December 28, 2001
 
This tutorial has definitely helped me for the project at hand.
-- Michael Manfre, January 06, 2002
 
Wonderful tutorial...very useful info...
-- David Cyrille, January 08, 2002
 
How could this be adapted so that a common component used in both ASP.NET and a local application can serialize its configuration to the ASP.NET application's or local application's configuration file? For instance, I want to serialize the common component (a configuration class) to Web.config for ASP.NET applications and to something like myApp.exe.config for my local application.
-- Heath Stewart, January 14, 2002
 
very good review of custom tags.
-- Joel Dietz, January 18, 2002
 
nice overview, very easy to follow
-- Ben Ratzlaff, January 25, 2002
 
Great tutorial
-- Krishnan Subramanian, January 29, 2002
 
Good job with the tutorial.
-- Luis Ortiz, February 19, 2002
 
Good job!
-- Kuniaki Tran, March 03, 2002
 
Well done!
-- Sean Fitzgerald, March 14, 2002
 
Interesting, easy to follow, and well written. Thanks for the nice work.
-- Brian Malouf, March 23, 2002
 
easy to follow. i learned alot from this, not merely the content but also what good tutorials are.
-- Evan Deutsch, March 25, 2002
 
A good tutorial.
-- Brian Simoneau, April 04, 2002
 
Is there anyway to update entries on web.config programmatically?
-- Norman Fung, November 14, 2004
 
Copyright © 2001 DevHood® All Rights Reserved