国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Url Rewriting with Regex for ASP.NET 2.0(在asp.net2.0中使用正規(guī)表達(dá)式建立URL重寫)

2019-11-18 17:16:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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) 評(píng)論(1)  編輯 收藏 收藏至365Key 所屬分類: .net2.0
 
評(píng)論:
# re: Url Rewriting with Regex for ASP.NET 2.0(在asp.net2.0中使用正規(guī)表達(dá)式建立URL重寫) 2006-04-26 20:22 | AXii

哈哈哈,測(cè)試后  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    }   發(fā)現(xiàn)這個(gè)處理辦法對(duì)于虛擬路徑會(huì)出現(xiàn)轉(zhuǎn)發(fā)錯(cuò)誤,注意第2、3、9行,是我增加的,可以有效的解決虛擬路徑問(wèn)題。
 
2、無(wú)法滿足頁(yè)面回發(fā)的問(wèn)題!如何解決,還請(qǐng)您來(lái)修改:):


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平潭县| 肃宁县| 麻栗坡县| 华宁县| 九龙县| 彩票| 盐边县| 肥东县| 麻城市| 仪陇县| 焦作市| 津南区| 开平市| 湛江市| 襄汾县| 玛沁县| 乐安县| 商都县| 德钦县| 页游| 武陟县| 大足县| 门源| 淮安市| 棋牌| 托克逊县| 无极县| 平山县| 府谷县| 皮山县| 微博| 苍溪县| 淄博市| 鱼台县| 昭平县| 平顺县| 盖州市| 邳州市| 海宁市| 屏边| 博野县|