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

首頁 > 編程 > .NET > 正文

asp.net2.0使用正規(guī)表達式建立URL重寫

2024-07-10 13:10:59
字體:
來源:轉載
供稿:網友

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中使用正規(guī)表達式建立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    }   發(fā)現(xiàn)這個處理辦法對于虛擬路徑會出現(xiàn)轉發(fā)錯誤,注意第2、3、9行,是我增加的,可以有效的解決虛擬路徑問題。
 
2、無法滿足頁面回發(fā)的問題!如何解決,還請您來修改:):

,歡迎訪問網頁設計愛好者web開發(fā)。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 栾城县| 裕民县| 钦州市| 文化| 贵溪市| 普安县| 都兰县| 濮阳县| 平陆县| 班玛县| 蒙自县| 卢氏县| 温宿县| 庄浪县| 台北县| 三河市| 阿合奇县| 塔城市| 冷水江市| 乐亭县| 全南县| 塘沽区| 武穴市| 屯昌县| 浪卡子县| 阿巴嘎旗| 千阳县| 连平县| 根河市| 华亭县| 济阳县| 乌苏市| 东乡族自治县| 阿勒泰市| 临沭县| 崇明县| 龙山县| 全州县| 洞头县| 巴中市| 屏边|