WEB开发网      濠电姷鏁告繛鈧繛浣冲洤纾瑰┑鐘宠壘閻ょ偓銇勯幇鍫曟闁稿鍠愰妵鍕冀閵娧佲偓鎺楁⒒閸曨偄顏柡宀嬬畱铻e〒姘煎灡绗戦梻浣筋嚙濮橈箓顢氳濠€浣糕攽閻樿宸ュΔ鐘叉啞缁傚秹宕滆绾惧ジ寮堕崼娑樺缂佹宀搁弻鐔风暋閻楀牆娈楅梺璇″枓閺呯姴鐣疯ぐ鎺濇晝闁靛牆妫欓蹇旂節閻㈤潧浠﹂柛銊ョ埣楠炴劙骞橀鑲╋紱闂佽宕樼粔顔裤亹閹烘挸浜归梺缁樺灦閿曗晛螞閸曨垱鈷戦柟鑲╁仜婵″ジ鎮楀☉鎺撴珖缂侇喖顑呴鍏煎緞濡粯娅囬梻浣瑰缁诲倿寮绘繝鍥ㄦ櫇闁稿本绋撻崢鐢告煟鎼淬垻鈯曢柨姘舵煟韫囥儳绋荤紒缁樼箖缁绘繈宕橀妸褌绱濋梻浣筋嚃閸ㄤ即宕弶鎴犳殾闁绘梻鈷堥弫鍌炴煕閳锯偓閺呮瑧妲愬Ο琛℃斀闁绘劕妯婇崵鐔封攽椤旇棄鍔ら摶鐐烘煕閺囥劌澧柛娆忕箻閺屽秹宕崟顒€娅g紓浣插亾濠㈣泛顑囩粻楣冩煙鐎涙ḿ绠橀柨娑樼У椤ㄣ儵鎮欓鍕紙闂佽鍠栫紞濠傜暦閹偊妲诲┑鈩冨絻椤兘寮诲☉銏犖╅柕澶堝労閸斿绱撴担绋库偓鍝ョ矓瑜版帒鏋侀柟鍓х帛閺呮悂鏌ㄩ悤鍌涘 ---闂傚倸鍊烽悞锔锯偓绗涘厾娲煛閸涱厾顔嗛梺璺ㄥ櫐閹凤拷
开发学院WEB开发ASP.NET Url Rewriting with Regex for ASP.NET 2.0(在asp... 阅读

Url Rewriting with Regex for ASP.NET 2.0(在asp.net2.0中使用正规表达式建立URL重写)

 2006-04-27 17:06:52 来源:WEB开发网 闂傚倸鍊风欢姘缚瑜嶈灋闁圭虎鍠栫粻顖炴煥閻曞倹瀚�闂傚倸鍊风粈渚€骞夐敓鐘插瀭闁汇垹鐏氬畷鏌ユ煙閹殿喖顣奸柛搴$У閵囧嫰骞掗幋婵冨亾閻㈢ǹ纾婚柟鐐灱濡插牊绻涢崱妤冃℃繛宀婁簽缁辨捇宕掑鎵佹瀸闂佺懓鍤栭幏锟�濠电姷鏁告慨顓㈠箯閸愵喖宸濇い鎾寸箘閹规洟姊绘笟鈧ḿ褍煤閵堝悿娲Ω閳轰胶鍔﹀銈嗗笂閼冲爼鍩婇弴銏$厪闁搞儮鏅涙禒褏绱掓潏鈺佷槐闁轰焦鎹囬弫鎾绘晸閿燂拷闂傚倸鍊风欢姘缚瑜嶈灋闁圭虎鍠栫粻顖炴煥閻曞倹瀚�  闂傚倸鍊烽懗鑸电仚缂備胶绮〃鍛村煝瀹ュ鍗抽柕蹇曞У閻庮剟姊虹紒妯哄闁圭⒈鍋嗛惀顏囶樄闁哄本娲樼换婵婄疀閺囩姷鐛ラ梻浣哄帶婢瑰﹥绂嶅⿰鍫氣偓鏃堝礃椤忎礁浜鹃柨婵嗛婢ь喖霉閻樻瑥瀚粻楣冩煕椤愩倕鏋庨柣蹇嬪劜閵囧嫰寮村Ο鍝勫Е濡炪們鍨洪悷鈺呭箖閳╁啯鍎熼柕鍥у簻閹凤拷
核心提示: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,

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

Tags:Url Rewriting with

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接