用Global.asax实现伪静态.

在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)