Click here to Skip to main content
15,892,643 members
Articles / Web Development / ASP.NET

Page Template Framework for ASP.NET 1.1

Rate me:
Please Sign up or sign in to vote.
4.64/5 (63 votes)
16 Nov 20048 min read 298.1K   5.3K   168  
The Page Template Framework for ASP.NET 1.1 provides a configurable solution for creating page templates in a Web application. Using this framework, page templates are stored in an XML file, and can be dynamically configured without recompiling the Web application.
using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;

namespace Boardworks.Web.UI.PageFramework.Config
{
	/// <summary>
	/// Represents the Page.config XML file as a .NET class
	/// </summary>
	[XmlRoot()]
	public class PageConfig
	{
		/// <summary>
		/// A constant denoting the Page.config file name
		/// </summary>
		[XmlIgnore()]
		public const string PageConfigFileName = "Page.config";


		/// <summary>
		/// A constant denoting the PageConfig Cache object key
		/// </summary>
		[XmlIgnore()]
		public const string PageConfigCacheKey = "__PAGECONFIG";
	

		/// <summary>
		/// A constant denoting the Page.config file path
		/// </summary>
		[XmlIgnore()]
		public static string PageConfigFilePath 
		{
			get
			{
				// Return the default path to the Page.config file
				return String.Format("~/{0}", PageConfig.PageConfigFileName);
			}
		}


		/// <summary>
		/// The Page Templates defined in the Page.config file
		/// </summary>
		[XmlArray()]
		[XmlArrayItem("PageTemplate", typeof(PageTemplate))]
		public PageTemplateCollection PageTemplates;


		/// <summary>
		/// The Pages defined in the Page.config file
		/// </summary>
		[XmlArray()]
		[XmlArrayItem("Page", typeof(Page))]
		public PageCollection Pages;


		/// <summary>
		/// Default Constructor
		/// </summary>
		public PageConfig()
		{
			// Create a default PageTemplateCollection
			this.PageTemplates = new PageTemplateCollection();

			// Create a default PageCollection
			this.Pages = new PageCollection();
		}


		/// <summary>
		/// Locates the first Page in the PageConfig with the specified path
		/// </summary>
		/// <param name="pagePath">The path of the Page to locate</param>
		/// <returns>The Page found in the PageConfig instance</returns>
		public Page FindPage(string pagePath)
		{
			// Create a local variable to hold the Page when found
			Page pageFound = null;

			// Iterate over the Pages ArrayList
			foreach (Page page in this.Pages)
			{
				// Check to ensure the Page's Path is not a null value
				if (page.Path != null)
				{
					// Check to see if the current Page's Path matches the specified pagePath
					if (page.Path.Trim().ToUpper().Equals(pagePath.Trim().ToUpper()))
					{
						// The Page was found, obtain a reference to the Page
						pageFound = page;

						// Break from the loop
						break;
					}
				}

				//
				// TESTING
				//

				else if (page.Expression != null)
				{
					// Check to see if the current Page's Expression matches the specified pagePath
					if (Regex.IsMatch(pagePath, page.Expression, RegexOptions.IgnoreCase))
					{
						// The Page matched the Expression, obtain a reference to the Page
						pageFound = page;

						// Break from the loop
						break;
					}
				}
			}

			// Return the found Page
			return pageFound;
		}


		/// <summary>
		/// Locates the first Page in the PageConfig based on the path of the specified Request
		/// </summary>
		/// <param name="request">The Request object containing the current Page path</param>
		/// <returns>The Page found in the PageConfig instance</returns>
		public Page FindPage(System.Web.HttpRequest request)
		{
			// Call the FindPage method and specify the Request's path, replacing the ApplicationPath with a tilde
			return this.FindPage(request.Path.ToUpper().Replace(request.ApplicationPath.ToUpper(), "~"));
		}


		/// <summary>
		/// Locates the specified templateName in the Page's PageTemplates collection
		/// </summary>
		/// <param name="templateName">The template to be located</param>
		/// <returns>A reference ot the specified PageTemplate</returns>
		public PageTemplate FindTemplate(string templateName)
		{
			// Create a local variable to hold the PageTemplate if found
			PageTemplate templateFound = null;

			// Iterate over the templates in the PageTemplates ArrayList
			foreach (PageTemplate template in this.PageTemplates)
			{
				// Check to see if the curren PageTemplate matches the specified template
				if (template.Name.Trim().ToUpper().Equals(templateName.Trim().ToUpper()))
				{
					// The PageTemplate was found, obtain a reference to the template
					templateFound = template;

					// Break from the loop
					break;
				}
			}

			// Return the found PageTemplate
			return templateFound;
		}


		/// <summary>
		/// Locates the default PageTemplate specified for PageConfig
		/// </summary>
		/// <returns>A reference to the default PageTemplate</returns>
		public PageTemplate FindDefaultTemplate()
		{
			// Create a local variable to hold the default PageTemplate
			PageTemplate defaultTemplate = null;

			// Iterate over the templates in the PageTemplates ArrayList
			foreach (PageTemplate template in this.PageTemplates)
			{
				// Check to see if the current template is the default template
				if (template.IsDefault)
				{
					// Obtain a reference to the default template
					defaultTemplate = template;
				}
			}

			// Return the default PageTemplate
			return defaultTemplate;
		}


		/// <summary>
		/// Saves the PageConfig instance to the specified path
		/// </summary>
		/// <param name="configFilePath">The path to which the PageConfig will be saved</param>
		public void Save(string configFilePath)
		{
			// Create an XmlSerializer to save the Page.config file
			XmlSerializer xs = new XmlSerializer(typeof(PageConfig));

			// Create an XmlTextWriter for saving the serialized XML to a file
			XmlTextWriter writer = new XmlTextWriter(configFilePath, System.Text.Encoding.UTF8);

			// Set the formatting mode for the writer
			writer.Formatting = Formatting.Indented;

			// Serialize the PageConfig into the writer
			xs.Serialize(writer, this);

			// Flush the writer
			writer.Flush();

			// Close the writer
			writer.Close();
		}


		/// <summary>
		/// Returns an XML string representing the serialized PageConfig
		/// </summary>
		/// <returns>An XML string representing the serialized PageConfig</returns>
		public override string ToString()
		{
			// Create an XmlSerializer to generate the Page.config XML string
			XmlSerializer xs = new XmlSerializer(typeof(PageConfig));

			// Create a MemoryStream to serialize the instance into
			MemoryStream ms = new MemoryStream();

			// Serialize the instance into the MemoryStream
			xs.Serialize(ms, this);

			// Get the XML string from the MemoryStream
			return System.Text.Encoding.UTF8.GetString(ms.ToArray());
		}


		/// <summary>
		/// Deserializes the specified configuration file into a new instance 
		/// of the PageConfig class
		/// </summary>
		/// <param name="configPath">The path to the configuration file</param>
		/// <returns>The configuration file as an instance of the PageConfig class</returns>
		public static PageConfig Load(string configFilePath)
		{
			// Load the Page.config file into an XmlTextReader
			XmlTextReader reader = new XmlTextReader(configFilePath);

			// Create an XmlSerializer to load the Page.config file
			XmlSerializer xs = new XmlSerializer(typeof(PageConfig));

			// Deserialize the Page.config file into a PageConfig object
			PageConfig pc = (PageConfig)xs.Deserialize(reader);

			// Close the XmlTextReader
			reader.Close();

			// Return the deserialized instance
			return pc;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Scott Van Vliet is a Principal Consultant with Neudesic based in Irvine, CA. He has been designing and developing technology solutions for the past 8 years, and has worked with Microsoft .NET technologies for over 3 years. Scott is currently developing solutions with ASP.NET 2.0, C# 2.0, Windows Presentation Foundation (codename "Avalon"), SQL Server 2000/2005, Reporting Services and SharePoint.

Scott welcomes feedback and can be reached through his Weblog at http://weblogs.asp.net/skillet/.

Comments and Discussions