看了 Asp.net 静态页面生成(1)----模板生成 的朋友 肯定要问 那么第二种生成方式是怎么样的?
那就是 动态页生成
二、动态页面生成
动态页面生成,就是采用动态页面生成静态页面,主要用于已有的动态站点静态化。对原来站点改动较小
实现思想:重载动态页面的Render() 函数,在生成页面代码时将代码写到我们的静态页面中。可以参考 "孟子E " 的解决方法
还有就是修改链接,将原来的动态链接该为静态链接
优点:实现比较简单,对原来站点改动较小,生成管理器可以部署在客户端。
缺点:性能低、生成速度慢。
现在我们详细讲解如何将一个现有的网站更改为可以生成的。我们假设网站的分类只有一级,生成目录页和文章页。
首先建立一个页面基类 EnableCreated 类 实例代码:
然后需要生成的页面继承这个基类
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
/// 可以生成静态页的页面基类
/// </summary>
public
class
EnableCreated : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string _CreatedFileName;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 生成后的静态页面相对文件名(包含路径)
/// </summary>
protected string CreatedFileName
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _CreatedFileName;
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_CreatedFileName = value;
}
}
public EnableCreated()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected override void Render(HtmlTextWriter writer)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//如果存在生成参数,并且值为true 就生成
if (!string.IsNullOrEmpty(Request.QueryString["Create"]) && Request.QueryString["Create"].ToLower() == "true")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.IO.StringWriter html = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html);
base.Render(tw);
System.IO.StreamWriter sw;
sw = new System.IO.StreamWriter(Server.MapPath("CreatedFileName"), false, System.Text.Encoding.UTF8);
sw.Write(html.ToString());
sw.Close();
tw.Close();
//清除生成的代码,仅输入1 用于计算生成结果
Response.Clear();
Response.Write(1);
Response.End();
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//生成失败时 输出0
Response.Clear();
Response.Write(0);
Response.End();
}
}
//正常工作
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.Render(writer);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 生成文章链接
/// </summary>
/// <param name="articleId"></param>
/// <returns></returns>
protected string MakeArticleUrl(int articleId)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!string.IsNullOrEmpty(Request.QueryString["Create"]) && Request.QueryString["Create"].ToLower() == "true")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "静态页面地址";//代码省略
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "动态页面地址";//代码省略
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 生成目录链接
/// </summary>
/// <param name="categoryId"></param>
/// <returns></returns>
protected string MakeCategoryUrl(int categoryId)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!string.IsNullOrEmpty(Request.QueryString["Create"]) && Request.QueryString["Create"].ToLower() == "true")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "静态页面地址";//代码省略
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "动态页面地址";//代码省略
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
文件名:Category.aspx
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Category.aspx.cs" Inherits="Category" EnableViewState="false"
%>
<!
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:GridView
ID
="gvArticle"
runat
="server"
>
<
Columns
>
<
asp:TemplateField
>
<
ItemTemplate
>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<
asp:HyperLink
ID
="hfArticleTitle"
runat
="server"
Text
='<%#
Eval("Title") %
>
' NavigateUrl='
<%
# MakeArticleUrl((int)Eval("ArticleId"))
%>
' />
</
ItemTemplate
>
</
asp:TemplateField
>
</
Columns
>
</
asp:GridView
>
</
div
>
</
form
>
</
body
>
</
html
>
文件名:Category.aspx.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
partial
class
Category : EnableCreated
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!string.IsNullOrEmpty(Request.QueryString["Create"]) && Request.QueryString["Create"].ToLower() == "true")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.CreatedFileName = "~/Article/Category_" + Request.QueryString["id"] + ".html";
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
周末了,写点开发经验大家分享
废话少说,开门见山。静态页面生成技术是很多大网站采用的技术。用于大型网站的访问量特别高,采用动态页面难免服务器负担很重,负载大的时候可能down机。人们想出了采取以前的静态页面,这样可以减少服务器运算负载,但是一个一个页面的做肯定人工成本太高。于是乎就出现了静态页面生成技术。
静态页面生成的实现方法大致可以分为两种
一、模板生成
实现思想:提前把网页的公共部分写好,做成一个模板,而不同的部分采用特殊字符代替。当需要生产的时候,用程序去读取模板,然后去数据库中找到需要的数据替换模板中的特殊字符。生成真正的网页然后存在网站的目录下。
有点:效率高,生成速度快。
缺点:需要在网站建设之前就确定采用此方法生成。对于已建好的动态站点改版为此方法 工作量大。
实例代码:
Article.Templete.txt
<!
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
>
<
title
><
@Title
@
></
title
>
</
head
>
<
body
>
<
table
border
="0"
cellpadding
="0"
cellspacing
="0"
width
="980"
>
<
tr
>
<
td
style
="height: 200px"
>
header
</
td
>
</
tr
>
<
tr
>
<
td
>
<
table
border
="0"
cellpadding
="0"
cellspacing
="0"
width
="980"
>
<
tr
>
<
td
>
<
@Title
@
>
</
td
>
</
tr
>
<
tr
>
<
td
>
<
@Content
@
>
</
td
>
</
tr
>
<
tr
>
<
td
>
剪辑:
<
@Editor
@
>
访问次数:
<
@Hits
@
>
</
td
>
</
tr
>
</
table
>
</
td
>
</
tr
>
<
tr
>
<
td
style
="height: 100px"
>
footer
</
td
>
</
tr
>
</
table
>
</
body
>
</
html
>
生成函数
/**/
/// <summary>
/// 生成文章方法
/// </summary>
/// <param name="templetefile">模板文件路径</param>
/// <param name="articleId">文章Id</param>
public
void
MakeArticleFile(
string
templetefile,
int
articleId)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string sqlStr = "SELECT * FROM Article WHERE ArticleId = " + articleId.ToString();
DataSet ds = DataBase.ExecuteDataset(sqlStr);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataRow dr = ds.Tables[0].Rows[0];
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.IO.StreamReader sr = new System.IO.StreamReader(templetefile, System.Text.Encoding.UTF8);
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("/Article/" + dr["ArticleId"].ToString() + ".html"), false, System.Text.Encoding.UTF8);
string templete = sr.ReadToEnd();
sr.Close();
sr.Dispose();
templete = templete.Replace("<@Title@>", dr["Title"].ToString());
templete = templete.Replace("<@Content@>", dr["Content"].ToString());
templete = templete.Replace("<@Editor@>", dr["Editor"].ToString());
templete = templete.Replace("<@Hits@>", dr["Hits"].ToString());
sw.Write(templete);
sw.Flush();
sw.Close();
sw.Dispose();
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(ex.Message);
}
}
}
然后再访问这个页面的时候带上一个参数 Category.aspx?id=12&Create=true
从服务段可以得到一个数字 1 或者 0,1表示生成成功,0表示生成失败。
注意 第一行
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Category.aspx.cs" Inherits="Category" EnableViewState="false" %>
中的EnableViewState="false" 。因为要生成静态页面阿。所以这个地方就不需要 ViewState 了
下一节讲解如何编写 生成器 待续