Url Rewriting with Regex for ASP.NET 2.0(在asp.net2.0中使用正规表达式建立URL重写)
作者:互联网
2009-06-30
A new feature in Asp.Net 2.0 is it's built-in url rewriting support. When i looked into this new feature i found out it lacked regular expressions support, wich is really the point of an Url Mapper. ScottGlu at his blog, explains the reason why the Asp.Net team didn't implemented this feature. Basically they realized that a full featured version would want to take advantage of the next IIS 7.0 new features, specially the support for all content-types (images and directories).
Anyway, it's really simple to implement a Url Rewriting Module with Regex support in Asp.Net. I wrote a quick and simple HttpModule for this. The whole magic is done within a few lines within the HttpModule:
1 public void Rewrite_BeginRequest(object sender, System.EventArgs args) {
2 string strPath = HttpContext.Current.Request.Url.AbsolutePath;
3 UrlRedirection oPR = new UrlRedirection();
4 string strURL = strPath;
5 string strRewrite = oPR.GetMatchingRewrite(strPath);
6 if (!String.IsNullOrEmpty(strRewrite)) {
7 strURL = strRewrite;
8 } else {
9 strURL = strPath;
10 }
11 HttpContext.Current.RewritePath("~" + strURL);
12 }
The code is self explanatory. When a request that is processed by the Asp.Net engine, the module checks an xml for a regex match. I've seen many Url Rewriting engines that uses Web.config to store the matching rules but i prefer using an additional xml file. The rewriting rules file look like the following:
1
2
3
4
5
6
7
8
9
10
11
The rule matching routine, wich is implemented in the GetMatchingRewrite() method is quite simple and lightweighted:
1 public string GetMatchingRewrite(string URL) {
2 string strRtrn = "";
3
4 System.Text.RegularExpressions.Regex oReg;
5
6 foreach (RedirectRule oRule in Rules) {
7
8 Reg = new Regex(oRule.URL);
9 Match oMatch = oReg.Match(URL);
10
11 if (oMatch.Success) {
12 strRtrn = oReg.Replace(URL, oRule.Rewrite);
13 }
14
15 }
16 return strRtrn;
17 }
I have uploaded a sample project that uses this rewriting engine. The HttpModule and it's helper classes are inside the App_Code folder. I hope you find this code useful, if you have any questions just leave a comment in this entry. Happy coding!
--------------------------------------------------------------------------------
FROM DEVEL.oping.net
posted on 2006-04-26 14:17 徐灿钊Asp.net专栏 阅读(48) 评论(1) 编辑 收藏 收藏至365Key 所属分类: .net2.0
评论:
# re: Url Rewriting with Regex for ASP.NET 2.0(在asp.net2.0中使用正规表达式建立URL重写) 2006-04-26 20:22 | AXii
哈哈哈,测试后 1 public void Rewrite_BeginRequest(object sender, System.EventArgs args)
2 {
3 string appPath = HttpContext.Current.Request.ApplicationPath;
4 HttpContext.Current.Response.Write(appPath + "
");
5
6 string strPath = HttpContext.Current.Request.Url.AbsolutePath;
7 HttpContext.Current.Response.Write(strPath + "
");
8
9 strPath = strPath.Substring(appPath.Length);
10
11 HttpContext.Current.Response.Write(strPath + "
");
12
13 UrlRedirection oPR = new UrlRedirection();
14
15 string strURL = strPath;
16
17 string strRewrite = oPR.GetMatchingRewrite(strPath);
18
19 if (!String.IsNullOrEmpty(strRewrite))
20 {
21 strURL = strRewrite;
22 }
23 else
24 {
25 strURL = strPath;
26 }
27
28 HttpContext.Current.RewritePath("~" + strURL);
29 } 发现这个处理办法对于虚拟路径会出现转发错误,注意第2、3、9行,是我增加的,可以有效的解决虚拟路径问题。
2、无法满足页面回发的问题!如何解决,还请您来修改:):
相关标签:
相关推荐
专题
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
最新数据
相关文章
详解.NET Core如何构建一个弹性的HTTP请求机制
.NET Core使用Redis实现创建分布式锁
.NET内存管理释放的两种方式
基于.NET8实现WinFrom应用窗口自动缩放功能
浅析如何在 ASP.NET Core中实现速率限制
.NET Core 委托原理解析(最新推荐)
.NET 9 new features-Microsoft.ML.Tokenizers 库(文本标记化功能)
如何在 .NET 中使用 Tesseract 识别图片文字
ASP.NET Core Web API之Token验证的实现
asp.net core web api项目添加自定义中间件的实现
AI精选
