在Global.asax中实现URL 的重写.

1 protected void Application_BeginRequest(object sender, EventArgs e)
2 {
3 string url = Request.RawUrl.ToString();
4 if (url.ToLower().IndexOf("page/bloglist/") > -1 && url.IndexOf("?") < 0)
5 {
6 int lastg = url.LastIndexOf("/");
7 int lastdot = url.LastIndexOf(".aspx");
8 string nurl = String.Format("{0}/blog.aspx?sid={1}", url.Substring(0, lastg), url.Substring(lastg + 1, lastdot - lastg - 1));
9 HttpContext.Current.RewritePath(nurl);
10 }
11
12 }

刚开始这段代码的意思是当请求到类似如:page/bloglist/123456789.aspx 的页面的时候,将其自动转向页面:

blog.aspx?sid=123456789 的页面.当然page/bloglist/123456789.aspx 这个页面 是实际不存在的,这也就是为什么 大家称这种方法是伪静态的原因了.

posted on 2011-03-22 14:34  $克  阅读(389)  评论(0编辑  收藏  举报

导航