asp.net 靜態頁面產生代碼

來源:互聯網
上載者:User

asp教程.net 靜態頁面產生代碼

目前網頁html靜態化是利用其它的動態技術產生html靜態頁面,還不是靜態網站。因為的確是這樣的,產生html靜態網頁有一定的好處。
     一、加快頁面開啟瀏覽速度,靜態頁面無需串連資料庫教程開啟速度較動態網頁面有明顯提高;
     二、有利於搜尋引擎最佳化seo教程,baidu、google都會優先收錄靜態頁面,不僅被收錄的快還收錄的全;
     三、減輕伺服器負擔,瀏覽網頁無需調用系統資料庫;
     四、網站更安全,html頁面不會受asp相關漏洞的影響;
     觀看一下大一點的網站基本全是靜態頁面,而且可以減少攻擊,防sql注入。資料庫出錯時,不影響網站正常訪問。
     產生html文章雖操作上麻煩些,程式上繁雜些,但為了更利於搜尋,為了速度更快些,更安全,這些犧牲還是值得的。
//產生靜態頁

 

    protected void gridview1_rowdeleting(object sender, gridviewdeleteeventargs e)

    {

        string n_id = this.gridview1.datakeys[e.rowindex]["nid"].tostring();

        sqlconnection conn = new sqlconnection("server=.;uid=sa;pwd=;database=dress");

        sqlcommand cmd = new sqlcommand("select nid,n_title,n_content,n_pic,n_date from p_news where nid="+n_id,conn);

        conn.open();

        sqldatareader dr = cmd.executereader();

 

        //擷取新聞發布的時間

        string date = "";

        if(dr.read())

        {

            date = convert.todatetime(dr["n_date"]).tostring("yyyymmddhhmmss_");

            //調用靜態產生的方法

            transferstatic(dr["n_title"].tostring(), dr["n_content"].tostring(), dr["n_pic"].tostring(), date, n_id);

            response.write("<script>alert('靜態頁面產生成功!');location='bulidstaticpage.aspx'</script>");

        }

    }

 

//轉換靜態方法

    public bool transferstatic(string title,string content,string pic,string date,string nid)

    {

        //輸出路徑

        string outpath = server.mappath("~/newdetails");

        //簡體中文

        encoding encoding = encoding.getencoding("gb2312");

        //讀模數版檔案

        string htmlmodel = server.mappath("~/htmlmodel/newdetail.html");

        streamreader sr = null;

        streamwriter sw = null;

        string str = "";//儲存內容的字串

        try

        {

            sr=new streamreader(htmlmodel,encoding);

            str=sr.readtoend();//從頭讀到尾

        }

        catch(exception e)

        {

            response.write(e.message);

            response.end();

            sr.close();

        }

 

 

//刪除指定的頁面

    protected void button2_click(object sender, eventargs e)

    {

        string path = server.mappath("~/newdetails");

        file.delete(path + "//" + this.listbox1.selectedvalue);

        //page.clientscript.registerclientscriptblock(this.gettype(), "資訊處理", "<script>alert('刪除成功!');location='bulidstaticpage.aspx'</script>");

        page.clientscript.registerstartups教程cript(this.gettype(), "資訊處理", "<script>alert('刪除成功!');location='bulidstaticpage.aspx'</script>");

    }

 

 

 

產生靜態頁列表 

 

 

//得到靜態頁面的路徑

    public string htmlpath(datetime date,string nid)

    {

        string path = "newdetails/" + date.tostring("yyyymmddhhmmss_") + nid + ".html";

        return path;

}

   

 

    //產生靜態列表

    protected void button1_click(object sender, eventargs e)

    {

        sqlconnection conn = new sqlconnection("server=.;uid=sa;pwd=;database=dress");

        sqldataadapter da = new sqldataadapter("select nid,n_title,n_content,n_pic,n_date from p_news",conn);

        dataset ds = new dataset();

        da.fill(ds);

 

        string str = "";

        for (int i = 0; i < ds.tables[0].rows.count;i++)

        {

          

 

 //取時間

            string date = convert.todatetime(ds.tables[0].rows[i]["n_date"]).tostring("yyyymmddhhmmss_");

            //取編號

            string nid = ds.tables[0].rows[i]["nid"].tostring();

            //構造檔案名稱

            string pagename = date + nid + ".html";

            //構造字串

            str += "<tr><td width=360px>"+"<a href='newdetails/"+pagename+"'>"+ds.tables[0].rows[i]["n_title"].tostring()+"</a>"+"</td><td width=200px>"+ds.tables[0].rows[i]["n_date"].tostring()+"</td></tr>";

        }

 

        //調用轉換方法產生靜態頁

        transferstatic(str);

    }

 

    //產生靜態頁

    public bool transferstatic(string strall)

    {

        //輸出路徑

        string outpath = server.mappath("~/");

        encoding encoding = encoding.getencoding("gb2312");

 

        //讀模數版檔案

        string htmlmode = server.mappath("~/htmlmodel/newslist.html");

        streamreader sr = null;

        streamwriter sw = null;

        string str = "";

        try

        {

            sr = new streamreader(htmlmode, encoding);

            str = sr.readtoend();

        }

        catch (exception e)

        {

            response.write(e.message);

            response.end();

            sr.close();

        }

        //構造要產生的靜態頁面的名字

        string pagename = "newslist.html";

        //開始替換內容

        str = str.replace("newslist", strall);

        //寫檔案

        try

        {

            sw = new streamwriter(outpath + pagename, false, encoding);

            sw.write(str);//將字串str寫到檔案

            sw.flush();

        }

        catch (exception e)

        {

            response.write(e.message);

            response.end();

        }

        finally

        {

            sw.close();

        }

        return true;

  }

 

在前台綁定新聞標題和時間:

        <table>
        <asp:repeater id="repeater1" runat="server">
            <itemtemplate>
                    <tr>
                        <td width="360px"><a href='<%# htmlpath(convert.todatetime(eval("n_date")),eval("nid").tostring())%>'><%# eval("n_title") %></a></td>
                        <td width="200px"><%# eval("n_date") %></td>
                    </tr>
            </itemtemplate>
        </asp:repeater>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.