首页 > 其他 > 详细

用Global.asax实现伪静态.

时间:2014-02-17 12:28:10      阅读:310      评论:0      收藏:0      [点我收藏+]

在Global.asax文件里添加Application_BeginRequest事件处理.添加如下代码:

 

1 protected void Application_BeginRequest(Object sender, EventArgs e)
2        {
3          string oldUrl = HttpContext.Current.Request.RawUrl;

5          string pattern = @"^(.+)/product/(\d+)$";
6          string replace = "$1/product.aspx?id=$2";
7      
8          if (Regex.IsMatch(oldUrl, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled))
9            {
10              string newUrl = Regex.Replace(oldUrl, pattern, replace, RegexOptions.Compiled | RegexOptions.IgnoreCase);
11              this.Context.RewritePath(newUrl);
12           }
13        } 
14

 

 

当请求类似xxxx/product/111的时候

 

相当于请求xxxx/product.aspx?id=111

 

搞定.实现伪静态.

 

好处:

 

1.对搜索引擎更加友好~~

 

2.对用户友好~~Url更加好记.

 

3.隐藏真实的url地址,安全性提高.

 

4.看起来酷毙了~~

用Global.asax实现伪静态.

原文:http://www.cnblogs.com/zhoulove/p/3551825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!