By Popular Demand... More HttpModule Sample Code

Several folks have asked for this, so here it is... the two other pieces of the ResourceRedirect solution from my LAYOUTS branding post. They're also the least interesting, which is why I didn't post them before. :)  Enjoy... review this post for the full context.

Redirect.cs

using

System;

using

System.Collections.Generic;

using

System.Text;

namespace

MOSS.Branding

{

public struct Redirect

{

public string pattern;

public string masterPageUrl;

public string originalMaster;

public string destinationPageUrl;

public Redirect(string _pattern, string _masterPageUrl, string _originalMaster, string _destinationPageUrl)

{

pattern = _pattern;

masterPageUrl = _masterPageUrl;

originalMaster = _originalMaster;

destinationPageUrl = _destinationPageUrl;

}

}

}

RedirectSectionHandler.cs

using

System;

using

System.Collections;

using

System.Collections.Generic;

using

System.Text;

using

System.Configuration;

using

System.Xml;

namespace

MOSS.Branding

{

class RedirectSectionHandler : IConfigurationSectionHandler

{

#region

IConfigurationSectionHandler Members

public object Create(object parent, object configContext, System.Xml.XmlNode section)

{

ArrayList coll = new ArrayList();

XmlElement root = (XmlElement)section;

string pattern = "";

string masterPageUrl = "";

string originalMaster = "";

string destinationPageUrl = "";

foreach (XmlNode parentNode in root.ChildNodes)

{

XmlNode patternNode = parentNode.Attributes.GetNamedItem(

"pattern");

if (patternNode != null) pattern = patternNode.InnerText.ToLower();

XmlNode originalMasterNode = parentNode.Attributes.GetNamedItem(

"originalMaster");

if (originalMasterNode != null) originalMaster = originalMasterNode.InnerText.ToLower();

XmlNode masterPageUrlNode = parentNode.Attributes.GetNamedItem(

"masterPageUrl");

if (masterPageUrlNode != null) masterPageUrl = masterPageUrlNode.InnerText.ToLower();

XmlNode destinationPageUrlNode = parentNode.Attributes.GetNamedItem(

"destinationPageUrl");

if (destinationPageUrlNode != null) destinationPageUrl = destinationPageUrlNode.InnerText.ToLower();

if ((masterPageUrl.Length == 0) && (destinationPageUrl.Length == 0)) throw new ConfigurationErrorsException("Redirects (masterPageUrl and destinationPageUrl) cannot both be blank.");

if ((pattern.Length == 0) && (originalMaster.Length == 0)) throw new ConfigurationErrorsException("Inputs (originalMaster and pattern) cannot both be blank.");

Redirect redirect =

new Redirect(pattern, masterPageUrl, originalMaster, destinationPageUrl);

coll.Add(redirect);

}

return coll;

}

#endregion

}

}

Comments