一个利用IIS404生成静态的思路

bingo_ 2010-09-19 10:54:06

<%@ WebHandler Language="C#" Class="_404_Install" %>

using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public class _404_Install : IHttpHandler {
public struct URL
{
public string VisURL;
public string ToURL;
}
public void ProcessRequest (HttpContext context) {
// 映射关系
List<URL> WURL = new List<URL>();
WURL.Add(new URL{ VisURL = "/news/[a-z]+([0-9]+)[a-z]+([0-9]*).html" , ToURL="/news.aspx?id=$1&type=$2"});
WURL.Add(new URL{ VisURL = "/class/b([0-9]+)/([0-9]+)/*", ToURL="/class.aspx?id=$1&page=$2"});
WURL.Add(new URL{ VisURL = "/index.html", ToURL="/default.aspx"});
context.Response.ContentType = "text/html";
// 用户访问的URL http://www.baidu.com/123/123.htm
string Url = context.Request.Url.ToString().Split(';')[1];
// 获得域名 http://www.baidu.com
string Domain = "http://" + context.Request.Url.Host;
// 获得完整目录 /123/123.htm
// 可能是 /123/123 或 /123/123/ 此时需要加index.html
string FullPath = Url.ToLower().Replace("http://", "");
int Tmp = FullPath.IndexOf("/");
if (Tmp > -1)
FullPath = FullPath.Substring(Tmp, FullPath.Length - Tmp);
else
FullPath = "";
// 处理 /123/123/ -> /123/123
if (FullPath[FullPath.Length - 1] == '/')
FullPath = FullPath.Substring(0, FullPath.Length - 1);
// 判断是否是目录 目录则添加默认页
string[] AP = FullPath.Split('/');
if (AP[AP.Length-1].IndexOf('.') == -1)
FullPath = FullPath + "/index.html";

for (int i = 0; i < WURL.Count; i++)
{
if (Regex.IsMatch(FullPath, WURL[i].VisURL))
{
string DURL = Regex.Replace(Url, WURL[i].VisURL, WURL[i].ToURL);
if (SavePageAsFile(DURL, FullPath))
context.Response.Redirect(FullPath);
else
context.Response.Write("该页不存在!");
context.Response.End();
}
}
context.Response.Write("该页不存在!");
}

public bool SavePageAsFile(string url, string Path)
{
try
{
string result = "";
WebRequest wr = WebRequest.Create(url);
WebResponse wp = wr.GetResponse();
using (StreamReader sr = new StreamReader(wp.GetResponseStream(), Encoding.GetEncoding("gb2312")))
result = sr.ReadToEnd();
wp.Close();
// 为防止生成不存在的信息 可以在这一步对内容进行筛选
// 创建目录
string Dir = Path.Substring(0, Path.LastIndexOf('/'));
Dir = HttpContext.Current.Server.MapPath(Dir);
if (!Directory.Exists(Dir))
Directory.CreateDirectory(Dir);
// 创建文件
using (StreamWriter sw = new StreamWriter( HttpContext.Current.Server.MapPath(Path) , true, Encoding.GetEncoding("gb2312")))
sw.Write(result);
return true;
}
catch (Exception e)
{
//HttpContext.Current.Response.Write(e.Message);
return false;
}
}

public bool IsReusable {
get {
return false;
}
}

}


总体思路大致如上 只需要将IIS里的404错误页执行为URL到此文件即可 优点是无须对全站进行改造 只需要改变URL为相应的静态地址即可
...全文
89 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bingo_ 2010-09-20
  • 打赏
  • 举报
回复
测试了运行了几天 暂时没有问题
dalmeeme 2010-09-19
  • 打赏
  • 举报
回复
赞一个,支持~~

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧