首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 软件教学-> 图文处理
url rewriting with regex for asp.net 2.0(在asp.net2.0中使用正规表达式建立url重写) _asp.net技巧
作者:网友供稿 点击:0
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 

A new feature in Asp.Net 2.0 is its 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 didnt 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, its 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. Ive 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 its 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、无法满足页面回发的问题!如何解决,还请您来修改:):


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Java Reflection (JAVA反射)-JSP教程,Java技巧及代码
·WebServices入门-.NET教程,Web Service开发
·如何在c#中使用mapx5.0(二)-.NET教程,C#语言
·用java启动记事本程序,并输入内容-JSP教程,Java技巧及代码
·正则表达式-ASP教程,正则表达式
·sql server中的外键约束-数据库专栏,SQL Server
·java中的基于套结字(socket)的通信 一. 一个双人单方向通信例子-JSP教程,Java技巧及代码
·图解利用Eclipse3+Lomboz3+Tomcat开发JSP --1.环境-JSP教程,Jsp/Servlet
·EditPlus+NAnt构建轻巧的.NET开发环境-.NET教程,Asp.Net开发
·ASP.NET2.0下利用javascript实现TreeView中的checkbox全选-.NET教程,Asp.Net开发
最新文章
·photoshop简单为美女照片瘦身及调色_photoshop教程
·fireworks巧绘精美可爱水杯图标_fireworks教程
·如何修复windows vista系统用户损坏的配置文件_windows vista
·本地门户营运的3个重点注意事项_站长心得
·必知:在线推广网站的十四种方法_站长心得
·google analytics统计分析新版本说明_google推广
·google adsense 违规行为集_google推广
·googleadsense的无效点击_google推广
·关于网络原创作品的版权问题_网络编辑
·符合xhtml标准div+css布局的网站对网站seo的影响_seo网站优化
相关主题
  • Url ReWriting 示例-ASP教程,性能优化
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭