方法一、将要访问的aspx页面内容读取出来,另存一下,保存为相应的html页面
如下:
首先建一个想要生成静态页的aspx页DynamicPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicPage.aspx.cs" Inherits="DynamicPage" %>
<!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>
我是个动态页
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="testid"
DataSourceID="SqlDataSource1" Width="386px">
<Columns>
<asp:BoundField DataField="testid" HeaderText="testid" InsertVisible="False" ReadOnly="True"
SortExpression="testid" />
<asp:BoundField DataField="testname" HeaderText="testname" SortExpression="testname" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [testid], [testname] FROM [test_table]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
然后建一个用于生成其它页面的Default.aspx,并且拖放一个按钮,然后在代码隐藏文件中键入如下代码:
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;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
GenerateStaticPage(Server.MapPath("GeneratePage/StaticPage.html"), "DynamicPage.aspx");
}
/// <summary>
/// 生成静态页
/// </summary>
/// <param name="destPage">生成后静态页的全路径名称</param>
/// <param name="srcPage">要生成的动态页</param>
private void GenerateStaticPage(string destPage,string srcPage)
{
StreamWriter sw = new StreamWriter(destPage, false, Encoding.GetEncoding("gb2312"));
Server.Execute(srcPage, sw);
sw.Close();
}
}
方法二、使用模板替换
如下:
模板textt.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ShowArticle</title>
<body>
biaoti
<br>
content<br>
author
</body>
</HTML>
biaoti
<br>
content<br>
author
</body>
</HTML>
//转载网上的,生成HTML页
public static bool WriteFile(string strText,string strContent,string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 写文件
try
{
sw = new StreamWriter(path + htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
此函数放在Conn.CS基类中了
在添加新闻的代码中引用 注:工程名为Hover
if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出错!");
}