advanced web statistics

Customize Web Applications using XML Configuration Files

3/6/2006 11:57:03 AM

Lately I have been developing larger and larger web applications or adding functionality to existing applications. Over the course of time these applications have either moved to new hosts, domain names, or in different directories within their existing domain. Anyone that has worked in web design / development can vouch for how big of a pain it is to go through each page and change the title, description, application path, etc... When designers first starting using CSS I was excited because this provided a quick and easy way to edit a webpage on a global scale. CSS enables the separation of document content (HTML, XHTML, etc...) from document presentation (CSS). The separation can improve content accessiblity, provide designer with more control, and most importantly (in my opinion) reduce the complexity and repetition in structural content.

web.config
The web.config file is, surprisingly enough, a configuration file for ASP.NET web applications. The configuration file is written in XML and consists of name-value pairs. The web.config file can be used to store data such as database connection strings, site titles and descriptions, application paths, and just about anything else you can think of. I like to think of my web.config as a ASP.NET application's CSS file. In the examples below I will show you how to customize your web application by reading data from a configuration file. First take a look at the example web.config file setup. The information you store in web.config will be safe as Internet Information Services (IIS) prevents direct browser access to configuration files.  HTTP access error 403 (forbidden) is returned when a browser attempts to directory request a configuration file.

<?xml version="1.0" encoding="utf-8"?>

<
configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

<
configSections>

<
sectionGroup name="settings">
<
section name="blogConfig" type="System.Configuration.NameValueSectionHandler,
System, Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089, Custom=null
"/>
</
sectionGroup>

</
configSections>
<
settings>
<
blogConfig>
<
add key="blogPath" value="/blog/" />
<
add key="siteTitle" value="Will Asrari ~ Bellingham, WA Web Developement (ASP.NET, VB .NET, C#)" />
<
add key="authorName" value="Will Asrari" />
</
blogConfig>
</
settings>

<
system.web></system.web>
<
appSettings></appSettings>

</
configuration>

Reading the Configuration File
Let's pretend that you want every page in your application to display your site title. Let's pretend that your site is titled the same as above (Will Asrari yada yada yada). With the code sample below we can easily read the data from the configuration file and display on your page.

using System.Collections;
using System.Collections.Specialized;

protected void Page_Load(object sender, EventArgs e)
{
   NameValueCollection blogConfig = (NameValueCollection)
   ConfigurationSettings.GetConfig("settings/blogConfig");

   string siteTitle = blogConfig["siteTitle"].ToString();
   Page.Title = siteTitle;
}

In order for this code to work, you need to add a runat="server" attribute to your opening HEAD tag. This is a default when starting an application in Visual Studio 2005. If you are creating your applications in Notepad, Visual Studio 2003, TextPad, or any other editor you will need to manually add this attribute.

<head runat="server">
<
title></title>
</head>

Adding More Customization
You can use the same code above, with a slight variation and variable addition to create an even more customized page title throughout your application.

protected void Page_Load(object sender, EventArgs e)
{
   NameValueCollection blogConfig = (NameValueCollection)
   ConfigurationSettings.GetConfig("settings/blogConfig");

   string siteTitle = blogConfig["siteTitle"].ToString();
   string authorName = blogConfig["authorName"].ToString();

   Page.Title = siteTitle + " - Blog of " + authorName;
}

This code will display the site title as "Will Asrari ~ Bellingham, WA Web Developement (ASP.NET, VB .NET, C#) - Blog of Will Asrari" in the browser window. I know this title is rather excessive but I am simply trying to prove a concept. I want to say that a search engine-optimized page title should be less than 65 characters. Don't quote me on that.

In the configuration file that I have provided above you will notice that there is a path /blog/. This is the directory where I keep my blog application. If I ever move the blog to a different directory (i.e. /apps/blog) then I can simply change this value and my XML writer and URL rewriting engine will work flawlessly.

I hope this will help some of you tidy up your code!

.NET, XML

kick it on DotNetKicks.com

Leave a Comment

   

  Enter the text to proceed!