Asp.net源码专业站
首页->新知实践->UrlReWrite(Url重写或伪静态)完美示例源码>>UrlRewriter/URLRewriter.Module.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:UrlReWrite(Url重写或伪静态)完美示例源码
当前文件:文件类型 UrlReWriter/UrlRewriter/URLRewriter.Module.cs[5K,2009-6-12 11:57:50]打开代码结构图
普通视图
		            
1using System; 2using System.Web; 3using System.Text.RegularExpressions; 4using System.Configuration; 5using URLRewriter.Config; 6 7namespace 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
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:UrlReWrite(Url重写或伪静态)完美示例源码
51Aspx.com 版权所有 CopyRight © 2006-2010. 京ICP备06046876号 本站法律顾问:ITlaw-庄毅雄律师
返回顶部
客户服务:点击这里进行客户咨询 业务合作:点击这里洽谈业务合作 合作热线:010-68880146