1、在web.config中添加以下项目。
<compilation>
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
</compilation>
<httpHandlers>
<add verb="*" path="*.html" type="System.Web.UI.PageHandlerFactory"/>
</httpHandlers>
2、重写处理方法
Namespace web
{
public class eastClass : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
Uri url = context.Request.Url;
string filepath = context.Request.Url.AbsolutePath;
filepath = context.Server.MapPath("//") + filepath.Replace("/", "//");
if (File.Exists(filepath) == false)
{
if (url.ToString().IndexOf("要处理的url中的关键字") > -1)
{
string uname = url.Segments[url.Segments.Length - 1];//取出带后缀文件名称
string id = uname.Substring(0, uname.IndexOf("."));//真实的文件名
if (id != "")
context.Response.Redirect("/xxx.aspx?id=" + id, true);//跳转
}
else
{
filepath = context.Server.MapPath("//") + "404.htm";
string tempstr;
using (StreamReader sr = new StreamReader(filepath, Encoding.Default))
{
tempstr = sr.ReadToEnd();
}
context.Response.Write(tempstr);
context.Response.End();
}
}
else
{
string tempstr;
using (StreamReader sr = new StreamReader(filepath, Encoding.UTF8))
{
tempstr = sr.ReadToEnd();
}
context.Response.Write(tempstr);
context.Response.End();
}
}
}
}
本文作者:网友 来源:网络