【原創】asp.net靜態頁面產生方案

來源:互聯網
上載者:User

靜態頁面產生方案
新聞表T_Artice
包含欄位如下:
Id  新聞ID
Title  標題
Content 內容
Date 日期
Author  作者
LinkHtml    對應靜態頁面
//HasUpdate 是否有更新

添加新聞時,產生其靜態頁面,並將連結地址記錄在資料庫中
更新新聞時,將當前新聞指向的靜態頁面刪除,並產生新的靜態頁面,然後更新連結地址並記錄在庫。

 

Theme.htm檔案代碼

<!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>
</head>
<body>
    <table>
        <tr>
            <td style="width: 100px">
                使用者名稱</td>
            <td style="width: 100px">@ppt[0]@
            </td>
        </tr>
        <tr>
            <td style="width: 100px">
                IP</td>
            <td style="width: 100px">@ppt[1]@
            </td>
        </tr>
        <tr>
            <td style="width: 100px">
                資訊</td>
            <td style="width: 100px">@ppt[2]@
            </td>
        </tr>
        <tr>
            <td style="width: 100px">
                頁面</td>
            <td style="width: 100px">@ppt[3]@
            </td>
        </tr>
    </table>
   
</body>
</html>

WriteHtml.aspx代碼

Random rand = new Random();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Ads.BLL.T_Log bllLog = new Ads.BLL.T_Log();
            DataSet ds = new DataSet();
            ds = bllLog.GetAllList();
            if (ds != null)
            {
                lblMsg.Text = "<table>";
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    string file = Write(row["UserName"].ToString(), row["UserIp"].ToString(), row["Page"].ToString(), row["Message"].ToString());
                    lblMsg.Text += "<tr><td><a href=" + file + " target=_blank>" + row["UserName"].ToString() + "</a>";
                }
                lblMsg.Text += "</table>";
            }
        }
    }

    private string Write(string userName,string ip,string page,string msg)
    {
        string filesName="";
        string[] format = new string[4];//定義和htmlyem標記數目一致的數組   
        StringBuilder htmltext=new StringBuilder();   
        try   
        {    
            using (StreamReader sr = new StreamReader(Server.MapPath("Theme.htm")))    
            {   
                String line;   
                while ((line = sr.ReadLine()) != null)   
                {    
                    htmltext.Append(line);   
                }   
                sr.Close();    
            }   
        }   
        catch   
        {    
            Response.Write("<Script>alert(~讀取檔案錯誤~)</Script>");   
        }

        format[0]=userName; 
        format[1]= ip;
        format[2]=msg;
        format[3]= page;
        for (int i = 0; i < 4; i++)
        {
            htmltext.Replace("@ppt[" + i + "]@", format[i]);
        }
        try   
        {
           
            string s = rand.Next(999999).ToString();
            string fileName = DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + s + ".htm";
            filesName = fileName;
            fileName = Server.MapPath(fileName);
            using(StreamWriter sw=new StreamWriter(fileName,false,System.Text.Encoding.GetEncoding("GB2312")))   
            {    
                sw.WriteLine(htmltext);    
                sw.Flush();    
                sw.Close();   
            }   
        }   
        catch   
        {   
            Response.Write ("The file could not be wirte:");   
        }
        return filesName;
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.