用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.看起来酷毙了~~
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.看起来酷毙了~~
相关文章推荐
- .Net Global.asax、httpModules伪静态功能实现
- 在global.asax中实现定时生成静态首页
- 在global.asax中实现定时生成静态首页 (asp.net C#)
- 在global.asax中实现定时生成静态首页 (asp.net C#)
- 在global.asax中实现定时生成静态首页
- 在global.asax中实现定时生成静态首页
- .net全局定时定期执行某些操作在Global.asax中具体实现
- .net全局定时定期执行某些操作在Global.asax中具体实现
- .net全局定时定期执行某些操作在Global.asax中具体实现
- 利用Global.asax的Application_BeginRequest 实现url 重写 无后缀
- Global.asax的Application_BeginRequest实现url重写无后缀的代码
- [ASP.NET2.0]Global.asax实现在线人数统计和站点访问总量
- 利用Global.asax的Application_Error实现错误记录,错误日志
- .net知识系列之七:底层解密(ASP.NET底层之旅,HttpApplication类实例,HttpApplication及Global.asax,实现自定义文件类型处理)
- 设置Global.asax实现简单的Url重写
- Global.asax的Application_BeginRequest实现url重写无后缀的代码
- 设置Global.asax实现简单的Url重写
- 通过Global.asax实现简单的url重写
- 用global.asax在ASP.NET实现无组件无扩展名伪静态