asp.net|靜態
環境: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
我找到我想用的方法了:
using System.Web.Hosting;
WebHost host = (WebHost)ApplicationHost.CreateApplicationHost(
typeof(WebHost), name, path);
public void DoRequest(string page, string query, TextWriter writer) {
HttpRuntime.ProcessRequest(new SimpleWorkerRequest(page, query, writer));
}
這樣我就可以直接把一個ASPX檔案產生HTML檔案了
FileStream fs = File.Open("Default.htm");
host.DoRequest("Default.aspx",string.Empty,fs);
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
%>
<!--#include file="conn.asp"-->
<%
dim str
function menu(id)
set rs=server.CreateObject("adodb.recordset")
sql="select * from menu where id1="&id&" order by url,id"
rs.open sql,conn,1,1
str=str+"<table border='0' cellspacing='0' cellpadding='0'>"+chr(13)
i=1
while not rs.eof
ChildCount=conn.execute("select count(*) from menu where id1="&rs("id"))(0)
if ChildCount=0 then
if i=rs.recordcount then
menutype="file1"
else
menutype="file"
end if
menuname="<a href='"&rs("url")&"' target='_blank'>"&rs("menuname")&"</a>"
onmouseup=""
else
if i=rs.recordcount then
menutype="menu3"
listtype="list1"
&rs("id")&"','b"&rs("id")&"');"
else
menutype="menu1"
listtype="list"
&rs("id")&"','b"&rs("id")&"');"
end if
menuname=rs("menuname")
end if
str=str+"<tr><td id='b"&rs("id")&"' class='"&menutype&"'"&onmouseup&">"&menuname&"</td></tr>"+chr(13)
if ChildCount>0 then
str=str+"<tr id='a"&rs("id")&"' style='display:none'><td class='"&listtype&"'>"+chr(13)
menu(rs("id"))
str=str+"</td></tr>"+chr(13)
end if
rs.movenext
i=i+1
wend
str=str+"</table>"+chr(13)
rs.close
set rs=nothing
end function
menu(0)
set fso=server.CreateObject("scripting.filesystemobject")
set f1=fso.opentextfile(server.MapPath("demo.htm"),2,true)
f1.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>")
f1.write("<html>")
f1.write("<head>")
f1.write("<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>")
f1.write("<title>哈哈~~~!這回速度超快了吧?</title>")
f1.write("<meta name='keywords' content='asp2004,樹,菜單,無限級'>")
f1.write("<meta name='author' content='小灰,quxiaohui_0@163.com'>")
f1.write("<link href='style.css' rel='stylesheet' type='text/css'>")
f1.write("<script language='javascript' src='function.js'></script>")
f1.write("</head>")
f1.write("<body>")
f1.write("<a href='http://asp2004.net' target='_blank'>http://asp2004.net</a>提供最新下載《<a href='menu.asp'>管理</a>》<br>")
f1.write(str)
f1.write("</body>")
f1.write("</html>")
f1.close
set fso=nothing
response.Redirect("demo.htm")
%>
不用模板,只用ASP+FSO產生靜態HTML頁的一個方法(對於內容密集型頁面特別適用)
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