您的位置:首页 > 编程语言 > ASP

asp.net中关于静态页面生成的代码实例

2011-04-15 10:54 736 查看
目前网页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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: