您的位置:首页 > 其它

用Global.asax实现伪静态.

2014-02-16 21:53 302 查看
在Global.asax文件里添加Application_BeginRequest事件处理.添加如下代码:

1 protected void Application_BeginRequest(Object sender, EventArgs e)
2 {
3 string oldUrl = HttpContext.Current.Request.RawUrl;
4
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.看起来酷毙了~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: