现在大型网站诸如新浪网等都是生成的静态页面,生成静态页面的好处我想大家也都知道,不仅可以减轻服务器压力,更有利于SEO。为了让大家了解静态页面生成的原理。在这里我提供了最简洁的代码,旨在明白实现生成静态页面的最基本原理,为实现复杂的静态生成技术打下基础。
源码下载地址:生成静态页面简化版
步骤导读
1 读取模板
2 建立新文件
3 将模版中内容写入到新文件中
一:首先建立一个模板页,名称为text.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>{$NewsTitle$}</title>
<meta name="Generator" content="EditPlus" />
<meta name="Author" content="" />
<meta name="Keywords" content="" />
<meta name="Description" content="" />
</head>
<body>
<div align="center">
<table cellspacing="0" cellpadding="0" width="567" border="0">
<tbody>
<tr>
<td bgcolor="#cbcbcb">
<table cellspacing="1" cellpadding="4" width="643" border="0">
<tbody>
<tr>
<td>
<div id="contents">
<div id="center">
<div style="text-align: center;">
<b>{$NewsTitle$}</b>
</div>
<div id="aa1">
</div>
<div style="padding-top: 20px; margin-top: 5px;">
{$NewsBody$}
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
二:建立生成静态页的类,CreateHtml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace Common
{
public class CreateHtml
{
/// <summary>
/// 当前模板
/// </summary>
private string TempHtml;//模板页
private string HtmlFilePath;//静态页路径
private string TemplatePath;//模板页路径
#region 创建静态页构造函数
/// <summary>
/// 创建静态页----构造函数(主要用来实例化赋初始值)
/// </summary>
/// <param name="_serverPath">获取服务器根目录</param>
public CreateHtml(string _serverPath)
{
TemplatePath = System.IO.Path.Combine(_serverPath, "Template/text.html");//获取模板默认文件夹路径(服务器根目录)
HtmlFilePath = System.IO.Path.Combine(_serverPath, "");//读取生成的静态页路径
ReadFile();//读取模板
}
#endregion
#region 读取模板信息
/// <summary>
/// 读取模板
/// </summary>
private void ReadFile()
{
string filePath = System.IO.Path.Combine(TemplatePath, "");//获取模板的相应模板页路径
StreamReader sr = new StreamReader(filePath, Encoding.UTF8);//将模板写入流中
TempHtml = sr.ReadToEnd();//获取模板页的信息
sr.Close();//关闭流对象
}
#endregion
#region 创建文章内容页
/// <summary>
/// 创建文章显示页
/// </summary>
public void CreateShowNews(string fileName, string[] newsList)
{
string tmpBody = ReplaceNewsDetail(TempHtml, newsList);//替换文章标签
SaveFile(fileName, tmpBody);//保存
}
#endregion
#region 加载文章内容页
public string LoadShowNews(string[] newsList)
{
return ReplaceNewsDetail(TempHtml, newsList);
}
#endregion
#region 替换文章内容页的标签
/// <summary>
/// 替换文章内容页
/// </summary>
/// <param name="tmpBody">要替换的页面</param>
/// <param name="newsList">文章数组</param>
/// <returns></returns>
private string ReplaceNewsDetail(string tmpBody, string[] newsList)
{
StringBuilder sb = new StringBuilder();
sb.Append(tmpBody);
sb.Replace("{$NewsTitle$}", newsList[0]);
sb.Replace("{$NewsBody$}", newsList[1]);
return sb.ToString();
}
#endregion
#region 保存文件到服务器
/// <summary>
/// 保存文件
/// </summary>
/// <param name="fileName">获取页面的名称</param>
/// <param name="pageStr">替换后页面</param>
private void SaveFile(string fileName, string pageStr)
{
string filePath = "";
filePath = System.IO.Path.Combine(HtmlFilePath, fileName+".html");
StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8);
sw.Write(pageStr);
sw.Close();
}
#endregion
}
}
三:建立生成静态页面,Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HtmlPager._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>生成静态页测试页面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
文章标题:
<asp:TextBox ID="txtTitle" Text="这是标题,可修改" runat="server"></asp:TextBox>
<br />
<br />
文章内容:
<asp:TextBox ID="txtContent" runat="server" Text="这是生成的静态页内容,此内容可以修改。" Height="93px" TextMode="MultiLine"
Width="231px"></asp:TextBox>
不支持html标签,为了简化没有使用文本编辑器。<br />
<br />
<asp:Button ID="Button1" runat="server" Text="生成静态页" οnclick="Button1_Click" />
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Common;
namespace HtmlPager
{
public partial class _Default : System.Web.UI.Page
{
#region 生成静态页面
protected void Button1_Click(object sender, EventArgs e)
{
CreateHtml ch = new CreateHtml(Server.MapPath("/"));
string[] nlist = { txtTitle.Text, txtContent.Text };//为了简化就不从数据库读取,直接建立一个数组。
string fileName = "xiaohei";//是生成的静态页文件名称,如:xiaohei.html
ch.CreateShowNews(fileName, nlist);
Response.Redirect(fileName+".html");
}
#endregion
}
}
为了简化程序和减少篇幅,没有用数据库读取信息,只是实现静态生成步骤原理。本示例是用vs2008+二层架构。因为没有数据库,只写了两个项目,因此只能是简化的三层架构。具体可参考源码。
详细源码下载:生成静态页面简化版