在 Global.asax.cs 中添加 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);
}
}
{
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.asax.cs代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace TestApp1
{
public class Global : System.Web.HttpApplication
{
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);
}
}
}
}
前台Global.asax 代码:
<%@ Application Codebehind="Global.asax.cs" Inherits="TestApp1.Global" Language="C#" %>
关于vs2005中 Global.asax 没有 Global.asax.cs 问题解决 方法见:http://www.cnblogs.com/wifi/archive/2011/12/01/2269969.html