asp.net生成靜態頁面的方法

方法1、將要訪問的aspx頁面內容讀取出來,另存一下,保存爲相應的html頁面html

以下:ide

首先建一個想要生成靜態頁的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">this

<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;spa

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");
  }code

  /// <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();
  }
}orm

方法2、使用模板替換server

以下:xml

模板textt.htmlhtm

<!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出錯!");     } 

相關文章
相關標籤/搜索