blob: daa4deae5c2026b4c64de0afb9a448337d22273a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
namespace OpenIdProviderWebForms.Code {
using System.Configuration;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
// nicked from http://www.codeproject.com/aspnet/URLRewriter.asp
public class URLRewriter : IConfigurationSectionHandler {
public static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected XmlNode rules = null;
protected URLRewriter() {
}
public static void Process() {
URLRewriter rewriter = (URLRewriter)ConfigurationManager.GetSection("urlrewrites");
string subst = rewriter.GetSubstitution(HttpContext.Current.Request.Path);
if (!string.IsNullOrEmpty(subst)) {
Logger.InfoFormat("Rewriting url '{0}' to '{1}' ", HttpContext.Current.Request.Path, subst);
HttpContext.Current.RewritePath(subst);
}
}
public string GetSubstitution(string path) {
foreach (XmlNode node in this.rules.SelectNodes("rule")) {
// get the url and rewrite nodes
XmlNode urlNode = node.SelectSingleNode("url");
XmlNode rewriteNode = node.SelectSingleNode("rewrite");
// check validity of the values
if (urlNode == null || string.IsNullOrEmpty(urlNode.InnerText)
|| rewriteNode == null || string.IsNullOrEmpty(rewriteNode.InnerText)) {
Logger.Warn("Invalid urlrewrites rule discovered in web.config file.");
continue;
}
Regex reg = new Regex(urlNode.InnerText, RegexOptions.IgnoreCase);
// if match, return the substitution
Match match = reg.Match(path);
if (match.Success) {
return reg.Replace(path, rewriteNode.InnerText);
}
}
return null; // no rewrite
}
#region Implementation of IConfigurationSectionHandler
public object Create(object parent, object configContext, XmlNode section) {
this.rules = section;
return this;
}
#endregion
}
}
|