王朝网络
分享
 
 
 

asp.net2.0使用正规表达式建立URL重写

王朝asp·作者佚名  2006-11-24
宽屏版  字体: |||超大  

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 <?xml version="1.0" encoding="utf-8" standalone="yes"?>

2 <urlrewrites>

3 <rule name="Category Page">

4 <url>/([a-zA-Z][\w-]{1,149})\.aspx</url>

5 <rewrite>/Default.aspx?Category=$1</rewrite>

6 </rule>

7 <rule name="Item Page">

8 <url>/([a-zA-Z][\w-]{1,149})/([a-zA-Z][\w-]{1,149})\.aspx</url>

9 <rewrite>/Default.aspx?Category=$1&amp;Item=$2</rewrite>

10 </rule>

11 </urlrewrites>

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 + "<br />");

5

6 string strPath = HttpContext.Current.Request.Url.AbsolutePath;

7 HttpContext.Current.Response.Write(strPath + "<br />");

8

9 strPath = strPath.Substring(appPath.Length);

10

11 HttpContext.Current.Response.Write(strPath + "<br />");

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、无法满足页面回发的问题!如何解决,还请您来修改:):

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有