利用Net自身进行伪静态处理

在 Global.asax 中添加 Application_BeginRequest 事件:

protected void Application_BeginRequest(object sender, EventArgs e)
{
string pathAndQuery = Request.Url.PathAndQuery.ToLower();
if (pathAndQuery.IndexOf(".html") > -1)
{
pathAndQuery = "~/" + pathAndQuery.Replace(".html", ".aspx");
HttpContext.Current.RewritePath(pathAndQuery);
}

}

这样就可以在浏览器地址栏里用http://localhost/1234/xxx.html 来访问你的 http://localhost/1234/xxx.aspx页面了,浏览器地址栏显示的是http://localhost/1234/xxx.html (页面带参数也是可以的)。

Global.aspx添加方法:

单击web应用右键,选择 添加-》新建项,在“添加项”对话框中的左侧选择“Web”,右侧选择“全局应用程序类”,单击“添加”按钮来添加Global.asax,然后在Global.asax.cs 中 添加 Application_BeginRequest 事件(把上面的代码复制过去就可了)。

我们可以根据上面的代码进行扩展,如:通过数据库读取伪静态规则,使用正则表达式转化URL!接下来就靠自己发挥了。

posted @ 2011-10-25 12:06  Crazy Coder  阅读(337)  评论(0编辑  收藏  举报