温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:UrlReWrite(Url重写或伪静态)完美示例源码
当前文件:
UrlReWriter/UrlRewriter/URLRewriter.Module.cs[5K,2009-6-12 11:57:50],打开代码结构图
UrlReWriter/UrlRewriter/URLRewriter.Module.cs[5K,2009-6-12 11:57:50],打开代码结构图1using System; 2
using System.Web; 3
using System.Text.RegularExpressions; 4
using System.Configuration; 5
using URLRewriter.Config; 6
7
namespace URLRewriter 8
{ 9
public class RewriterModule : IHttpModule 10
{ 11
public void Init(HttpApplication app) 12
{ 13
// WARNING! This does not work with Windows authentication! 14
// If you are using Windows authentication, change to app.BeginRequest 15
app.AuthorizeRequest += new EventHandler(this.URLRewriter); 16
} 17
18
protected void URLRewriter(object sender, EventArgs e) 19
{ 20
HttpApplication app = (HttpApplication) sender; 21
string requestedPath = app.Request.Path; 22
23
// get the configuration rules 24
UrlsCollection rules = UrlsConfig.GetConfig().Urls; 25
26
for (int i = 0; i < rules.Count; i++) 27
{ 28
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) 29
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].VirtualUrl) + "$"; 30
31
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); 32
if (re.IsMatch(requestedPath)) 33
{ 34
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].DestinationUrl)); 35
RewriterUtils.RewriteUrl(app.Context, sendToUrl); 36
break; 37
} 38
} 39
40
} 41
42
public void Dispose() { } 43
} 44
45
46
47
/// <summary> 48
/// Provides utility helper methods for the rewriting HttpModule and HttpHandler. 49
/// </summary> 50
/// <remarks>This class is marked as internal, meaning only classes in the same assembly will be 51
/// able to access its methods.</remarks> 52
internal class RewriterUtils 53
{ 54
RewriteUrl 101
102
/// <summary> 103
/// Converts a URL into one that is usable on the requesting client. 104
/// </summary> 105
/// <remarks>Converts ~ to the requesting application path. Mimics the behavior of the 106
/// <b>Control.ResolveUrl()</b> method, which is often used by control developers.</remarks> 107
/// <param name="appPath">The application path.</param> 108
/// <param name="url">The URL, which might contain ~.</param> 109
/// <returns>A resolved URL. If the input parameter <b>url</b> contains ~, it is replaced with the 110
/// value of the <b>appPath</b> parameter.</returns> 111
internal static string ResolveUrl(string appPath, string url) 112
{ 113
if (url.Length == 0 || url[0] != '~') 114
return url; // there is no ~ in the first character position, just return the url 115
else 116
{ 117
if (url.Length == 1) 118
return appPath; // there is just the ~ in the URL, return the appPath 119
if (url[1] == '/' || url[1] == '\\') 120
{ 121
// url looks like ~/ or ~\ 122
if (appPath.Length > 1) 123
return appPath + "/" + url.Substring(2); 124
else 125
return "/" + url.Substring(2); 126
} 127
else 128
{ 129
// url looks like ~something 130
if (appPath.Length > 1) 131
return appPath + "/" + url.Substring(1); 132
else 133
return appPath + url.Substring(1); 134
} 135
} 136
} 137
} 138
139
} 140






}
}