首页 > 编程 > .NET > 正文

asp.net2.0使用正规表达式建立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中使用正规表达式建立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、无法满足页面回发的问题!如何解决,还请您来修改:):

,欢迎访问网页设计爱好者web开发。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表