首先,新建一个类,如:类名为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>下面添加:
如此,一个简单的asp.net伪静态功能就实现了,适合新手朋友参考,高手请飘过。