动态页面静态化

1、拆中做法、 、 判断静态文件是否存在、不存在就新建一个(提前编辑好一个静态页模板

详细信息
书名:[$title]
作者:[$author]
出版社:[$publisher]
、最后根据占位符得到静态页面)。

存在就直接转向静态文件、 、

2、配置web.config文件、



3、实现 IHttphandler 类

public void ProcessRequest(HttpContext context)
{
context.Request.ContentEncoding = Encoding.GetEncoding(“utf-8”); // 编码格式 、 、
context.Response.ContentEncoding = Encoding.GetEncoding(“utf-8”);
string url = context.Request.RawUrl; // 获得请求的原始 url 、、
string temp = url.Remove(url.Length - 5);
StringBuilder sb = new StringBuilder();
foreach (char c in temp.Skip(temp.LastIndexOf(’_’)+1))
{
sb.Append(c.ToString());
} // 取出 id 、、
//context.Response.Write(sb);
string urlPath = “~/htmls/book” + sb;
lock(context.Application) // 应用程序一次只能访问一个。 。
{
if (!File.Exists(context.Server.MapPath(urlPath)))
{
//如果不存在当前路径对应的文件则生成文件
string strHtml = string.Empty;
using (StreamReader sr = new StreamReader(context.Server.MapPath("~/htmls/Template.htm")))
{
strHtml = sr.ReadToEnd();
}
strHtml = strHtml.Replace("[KaTeX parse error: Expected 'EOF', got '#' at position 12: title]", "C#̲高级编程"); …author]", “张小三”);
strHtml = strHtml.Replace("[$publisher]", “华尔街金融出版社”);
using(StreamWriter sw=new StreamWriter(context.Server.MapPath(urlPath))){
sw.Write(strHtml);
}
}

    }
    context.Server.Execute(urlPath);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值