Customized Tags Add in web.config

ASP.NET provides a configuration system we can use to keep our applications flexible at runtime. In this article we will examine some tips and best practices for using the configuration system for the best results.
The five sections of the Web.config file that are critical to the operation of the CSF connectors - <configSections>, <system.web><httpHanders>, <Microsoft.Csf>, <microsoft.web.services2>, and <appSettings>.

<appSettings> holds the path to the EnterpriseInstrumentation.config file, as well as additional configuration parameters:
<appSettings>
<add key="instrumentationConfigFile" value="C:\CsfConfig\EnterpriseInstrumentation.config" />
</appSettings>

The <appSettings> element of a web.config file is a place to store connection strings, server names, file paths, and other miscellaneous settings needed by an application to perform work. The items inside appSettings are items that need to be configurable depending upon the environment, for instance, any database connection strings will change as you move your application from a testing and staging server into production.
The web.config file in ASP.NET is the central location for your web applications configuration.
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.

using System;
using System.Collections;
using System.Xml;
using System.Configuration;
using System.Web.Configuration;

namespace MyTest {
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.

ASP.Net 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="MyTest">
<section name="pageStyle" type="MyTest.PageStyleHandler, PageStyle" />
</sectionGroup>
</configSections>

<MyTest>
<pageStyle backColour="blue" />
</MyTest>
</configuration>

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.

Using an ASPX page
Here is an example of our new custom handler in action:

<%@ Import Namespace="MyTest" %>
<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("MyTest/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 blue!</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.
Tags: , ,
Hot on Web:


About author