asp.net中关于静态页面生成的代码实例
生活随笔
收集整理的這篇文章主要介紹了
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('www.3ppt.com靜態頁面生成成功!');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>
???? 一、加快頁面打開瀏覽速度,靜態頁面無需連接數據庫教程打開速度較動態頁面有明顯提高;
???? 二、有利于搜索引擎優化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('www.3ppt.com靜態頁面生成成功!');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>
轉載于:https://www.cnblogs.com/manloudoushiwo/archive/2011/04/15/2016860.html
總結
以上是生活随笔為你收集整理的asp.net中关于静态页面生成的代码实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 3349 Snowflake S
- 下一篇: 我看三层架构