環境:microsoft .net framework sdk v1.1
os:windows server 2003 中文版
asp.net生成靜態html頁
在asp中實現的生成靜態頁用到的filesystemobject對象!
在.net中涉及此類操作的是system.io
以下是程序代碼 注:此代碼非原創!參考別人代碼
//生成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); file://模板頁中的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出錯!");
}
-------------------------------------------------------------------------
模板頁text.html代碼
-------------------------------------------------------------------------
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>showarticle</title>
<body>
biaoti
<br>
content<br>
author
</body>
</html>
------------------------------------------------------------------------
提示添加成功后會出以當前時間為文件名的html文件!上面只是把傳遞過來的幾個參數直接寫入了html文件中,在實際應用中需要先添加數據庫,然后再寫入html文件
而且需要把生成的文件名等寫入數庫以便以后調用等,此實例只是實現了根據提交過來參數替換模板中的相應的字段! 需要完善的地方很多!哪位有高見,歡迎賜教!
asp.net生成靜態html頁! 2004-06-01 13:36 kriss
asp生成靜態網頁的方法
隨著網站訪問量的加大,每次從數據庫讀取都是以效率作為代價的,很多用access作數據庫的更會深有體會,靜態頁加在搜索時,也會被優先考慮。互聯網上流行的做法是將數據源代碼寫入數據庫再從數據庫讀取生成靜態面,這樣無形間就加大了數據庫。將現有的asp頁直接生成靜態頁,將會節省很多。
下面的例子是將、index.asp?id=1/index.asp?id=2/index.asp?id=3/這三個動態頁面,分別生成ndex1.htm,index2.htm,index3.htm存在根目錄下面:
<%
dim strurl,item_classid,id,filename,filepath,do_url,html_temp
html_temp="<ul>"
for i=1 to 3
html_temp = html_temp&"<li>"
item_classid = i
filename = "index"&item_classid&".htm"
filepath = server.mappath("/")&"/"&filename
html_temp = html_temp&filepath&"</li>"
do_url = "http://"
do_url = do_url&request.servervariables("server_name")&"/main/index.asp"
do_url = do_url&"?item_classid="&item_classid
strurl = do_url
dim objxmlhttp
set objxmlhttp = server.createobject("microsoft.xmlhttp")
objxmlhttp.open "get",strurl,false
objxmlhttp.send()
dim binfiledata
binfiledata = objxmlhttp.responsebody
dim objadostream
set objadostream = server.createobject("adodb.stream")
objadostream.type = 1
objadostream.open()
objadostream.write(binfiledata)
objadostream.savetofile filepath,2
objadostream.close()
next
html_temp = html_temp&"<ul>"
%>
<%
response.write ( "成功生成文件:" )
response.write ( "<br>" )
response.write html_temp
%>
fso生成靜態html文件的時候替換模板標簽一直是一個很麻煩的問題,至少我是這么認為的,還要別外做一個模板,麻煩!,我今天看見有一個方法可以解決這個問題
如一個正常的index.asp頁面,并且用asp代碼調出數據庫中的內容,另建一個makehtml.asp的頁面,加入一個textarea域,假設為name="body",將index.asp在textarea里調出來,如:
<textarea name="body"><!--#include file="index.asp"--></textarea>,將這個textarea包含在表單中,在接收表單頁用創建fso對象,如下生成index.html文件!
<%
filename="../index.html"
if request("body")<>"" then
set fso = server.createobject("scripting.filesystemobject")
set fout = fso.createtextfile(server.mappath(""&filename&""))
fout.write request.form("body")
fout.close
set fout=nothing
set fso=nothing
end if
%>
這樣index.html文件就生成了,連模板都用不著,只要將正常情況下使用的asp文件讀取到textarea里就可以了,目前尚未發現問題!當然前提是服務器要支持fso
新聞熱點
疑難解答
圖片精選