ASP.NET使用HttpHandler进行页面静态化(自动生成页面)
这次的Demo是,一个根页面,点击链接创建子页面,子页面都是一个Template页面进行替换的
一个根页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="Static.List" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>体育新闻</h2>
<p><a href="News/Info_0.html">中国拳手</a></p>
<p><a href="News/Info_1.html">纳斯很少情绪激动</a></p>
</div>
</form>
</body>
</html>
新建一个类,做假数据用,代替数据库的表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Static
{
public class News
{
public int Id { get; set; }
public string Title { get; set; }
public string Time { get; set; }
public string Content { get; set; }
}
}
建一个类,使用List进行加载数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Static
{
public class NewsManager
{
public List<News> lists = new List<News>();
public NewsManager()
{
lists.Add(new News()
{
Id=1,
Time="1019年1月1日",
Content="拳手的内容",
Title="拳手的标题"
});
lists.Add(new News()
{
Id = 2,
Time = "10年1月1日",
Content = "情绪的内容",
Title = "情绪的标题"
});
}
}
}
新建一个News文件夹,建一个Tamplate的网页,用来规范网页
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div>
<p>{$Title}</p>
<p>{$Time}</p>
<p>{$Content}</p>
</div>
</body>
</html>
建一个类进行操作,继承IHttpHandlers
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace Static
{
public class FileStaticHandler : IHttpHandler
{
public bool IsReusable => true;
public void ProcessRequest(HttpContext context)
{
string url = context.Request.RawUrl;
//通过Url把id取到
int last = url.LastIndexOf("_");
int dot = url.LastIndexOf(".");
int newId = int.Parse(url.Substring(last + 1, dot - last - 1));
//生成路径
string userFilePath = context.Server.MapPath("~/News/Info_" + newId + ".html");
//如果此路径没文件的话,就生成
if (!File.Exists(userFilePath))
{
//我的加载在类里面写的
NewsManager newsManager = new NewsManager();
List<News> news = newsManager.lists;
//获取某个文件的路径
string tempPath = context.Server.MapPath("~/News/Template.html");
//把此文件里面的关键字替换掉
//把这个文件序列化保存
string tempHtml = ReadTemplate(tempPath);
tempHtml = tempHtml.Replace("{$Title}", news[newId].Title);
tempHtml = tempHtml.Replace("{$Time}", news[newId].Time);
tempHtml = tempHtml.Replace("{$Content}", news[newId].Content);
//输出此文件
WriteHtmlFile(userFilePath, tempHtml);
}
//输出文件
context.Response.WriteFile(userFilePath);
}
private void WriteHtmlFile(string userFilePath, string tempHtml)
{
FileStream fs = new FileStream(userFilePath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(tempHtml);
sw.Close();
fs.Close();
}
private string ReadTemplate(string tempPath)
{
if (!File.Exists(tempPath))
{
throw new Exception("新闻详情页面模板文件未找到!");
}
FileStream fs = new FileStream(tempPath, FileMode.Open);
StreamReader sr = new StreamReader(fs);
string tempHtml = sr.ReadToEnd();
sr.Close();
fs.Close();
return tempHtml;
}
}
}
既然用了Handler,就要进行配置文件
<system.webServer>
<handlers>
<add name="test" path="News/*.html" verb="*" type="Static.FileStaticHandler" />
</handlers>
</system.webServer>
根页面效果图(记得显示全部文件,否则会不显示)
点击了第一个链接,就生成了页面