首先新建一个类,如:类名为URLRerite ,让他继承IHttpHandlerFactory接口
实际代码如下:
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
/// <summary>
/// URLRerite 的摘要说明
/// </summary>
public class URLRerite : IHttpHandlerFactory
{
#region IHttpHandlerFactory 成员
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
string path = url;
string extend = Path.GetExtension(path);
string getFileName = Path.GetFileNameWithoutExtension(path);
string sendpath = path.Replace(extend, ".aspx");
string filepath = pathTranslated;
string qurstring = "";
if (context.Request.QueryString.Count > 0)
{
qurstring = context.Request.QueryString.ToString() ;
}
// 重写URL
filepath = context.Server.MapPath(sendpath);
context.RewritePath(sendpath, String.Empty, qurstring);
return PageParser.GetCompiledPageInstance(sendpath, filepath, context);
}
public void ReleaseHandler(IHttpHandler handler)
{
//throw new Exception("The method or operation is not implemented.");
}
#endregion
}
最后在web.config文件中的<system.web>下面写上
<httpHandlers>
<add verb="*" path="*.shtml" type="URLRerite"/>
</httpHandlers>
···
···