1: #region Usings
2:
3: using System;
4: using System.Collections.Specialized;
5: using System.Text.RegularExpressions;
6: using System.Web;
7:
8: #endregion
9:
10: namespace Site.HTTPModules
11: {
12: /// <summary>
13: /// Does Rewriting of the urls specified in the settings
14: /// </summary>
15: public class UrlRewrite:IHttpModule
16: {
17: #region Variables
18: private static StringDictionary Settings = null;
19: #endregion
20:
21: #region Public Functions
22: /// <summary>
23: /// Disposes of the object
24: /// </summary>
25: public void Dispose()
26: {
27: }
28:
29: /// <summary>
30: /// Initializes the object
31: /// </summary>
32: /// <param name="context"></param>
33: public void Init(HttpApplication context)
34: {
35: Settings = //Load settings here (key is input, value is output)
36: context.BeginRequest += new EventHandler(context_BeginRequest);
37: }
38: #endregion
39:
40: #region Events
41:
42: /// <summary>
43: /// Checks the url vs the settings and reroutes the item if need be.
44: /// </summary>
45: /// <param name="sender"></param>
46: /// <param name="e"></param>
47: void context_BeginRequest(object sender, EventArgs e)
48: {
49: HttpContext Context = ((HttpApplication)sender).Context;
50: string CurrentPath = Context.Request.Path.ToLowerInvariant();
51:
52: foreach (string Key in Settings.Keys)
53: {
54: Regex TempRegex = new Regex(Key);
55: if (TempRegex.IsMatch(CurrentPath))
56: {
57: Context.RewritePath(Settings[Key] + "?Path=" + CurrentPath);
58: }
59: }
60: }
61: #endregion
62: }
63: }