Preferred Language:
Listing 21.3 - UrlRemapper.cs
using System;
using System.Web;
using System.Xml;
using System.Web.Caching;
using System.Text.RegularExpressions;
namespace AspNetUnleashed
{
public class UrlRemapper : IHttpModule
{
public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(app_BeginRequest);
}
public void app_BeginRequest(Object s, EventArgs e)
{
// Get HTTP Context
HttpApplication app = (HttpApplication)s;
HttpContext context = app.Context;
// Get current URL
string currentUrl = context.Request.AppRelativeCurrentExecutionFilePath;
// Get URL Mappings
XmlDocument urlMappings = GetUrlMappings(context);
// Compare current URL against each URL from mappings file
XmlNodeList nodes = urlMappings.SelectNodes("//add");
foreach (XmlNode node in nodes)
{
string url = node.Attributes["url"].Value;
string mappedUrl = node.Attributes["mappedUrl"].Value;
if (Regex.Match(currentUrl, url, RegexOptions.IgnoreCase).Success)
context.RewritePath(mappedUrl);
}
}
private XmlDocument GetUrlMappings(HttpContext context)
{
XmlDocument urlMappings = (XmlDocument)context.Cache["UrlMappings"];
if (urlMappings == null)
{
urlMappings = new XmlDocument();
string path = context.Server.MapPath("~/UrlMappings.config");
urlMappings.Load(path);
CacheDependency fileDepend = new CacheDependency(path);
context.Cache.Insert("UrlMappings", urlMappings, fileDepend);
}
return urlMappings;
}
public void Dispose() { }
}
}
Need ASP.NET and Visual Studio 2008 Training?
- Learn ASP.NET 3.5 from Stephen Walther, author of ASP.NET 3.5 Unleashed.
We've provided ASP.NET training for NASA, Lockheed Martin, the National Science Foundation, Verizon,
Boeing, the US House of Representatives, Kaiser, Petco, Mary Kay, and Microsoft.
Why not your company?
-
Receive a four day, hands-on, intensive workshop.
-
We fly to you, anywhere in the world.
-
We can bring our own laptops.
To learn more, visit the
Superexpert Training website.