【摘 要】利用如Dw-Mx這樣的工具產生html格式的模板,在需要添加格式的地方加入特殊標記(如$),動態組建檔案時利用代碼讀取此模板,然後獲得前台輸入的內容,添加到此模板的標記位置中,產生新檔案名稱後寫入磁碟,寫入後再向資料庫中寫入相關資料。
asp.net動態產生html頁面
此功能適用於後台資料庫功能不強的web網站,即大部分文本不是存放在資料庫的記錄中,而是放在html檔案或者xml檔案中,僅僅把索引放到資料庫中,如文章標題、類別、查詢關鍵字等。這樣適合於後台沒有諸如ms sql server這樣的資料庫支援的web網站。
適用於新聞發布系統,比如sina、163等都是採用動態產生html頁面的。
適用於需動態定製頁面的程式。比如論壇、聊天室等。可以載入定製好的html頁面,來加強美觀。
思路
1. 利用如Dw-Mx這樣的工具產生html格式的模板,在需要添加格式的地方加入特殊標記(如$),動態組建檔案時利用代碼讀取此模板,然後獲得前台輸入的內容,添加到此模板的標記位置中,產生新檔案名稱後寫入磁碟,寫入後再向資料庫中寫入相關資料。
2. 使用後台代碼寫入程式碼Html檔案,可以使用HtmlTextWriter類來寫html檔案。
優點
1. 可以建立非常複雜的頁面,利用包含js檔案的方法,在js檔案內加入document.write()方法可以在所有頁面內加入如頁面頭,廣告等內容。
2. 靜態html檔案利用MS Windows2000的Index Server可以建立全文檢索搜尋引擎,利用asp.net可以以DataTable的方式得到搜尋結果。而Win2000的Index服務無法尋找xml檔案的內容。如果包括了資料庫搜尋與Index索引雙重尋找,那麼此搜尋功能將非常強大。
3. 節省伺服器的負荷,請求一個靜態html檔案比一個aspx檔案伺服器資源節省許多。
缺點
思路二: 如果用硬式編碼方式,工作量非常大,需要非常多的html代碼。調試困難。而且使用寫入程式碼產生的html樣式無法修改,如果網站更換樣式,那麼必須得重新編碼,給後期帶來巨大的工作量。
因此這裡採用的是第一種思路
示列代碼
1.定義(template.htm)html模板頁面
<html>
<head>
<title>www.webjx.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body >
<table height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000">
<tr>
<td width="100%" valign="middle" align="left">
<span style="color: ;font-size: "></span>
</td>
</tr>
</table>
</body>
</html>
2.asp.net代碼:
//---------------------讀html模板頁面到stringbuilder對象裡----
string[] format=new string[4];//定義和htmlyem標記數目一致的數組
StringBuilder htmltext=new StringBuilder();
try
{
using (StreamReader sr = new StreamReader("存放模板頁面的路徑和頁面名"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}
}
catch
{
Response.Write("<Script>alert('讀取檔案錯誤')</Script>");
}
//---------------------給標記數組賦值------------
format[0]="background=\"bg.jpg\"";//背景圖片
format[1]= "#990099";//字型顏色
format[2]="150px";//字型大小
format[3]= "<marquee>產生的模板html頁面</marquee>";//文字說明
//----------替換htm裡的標記為你想加的內容
for(int i=0;i<4;i++)
{
htmltext.Replace("["+i+"]",format[i]);
}
//----------產生htm檔案------------------――
try
{
using(StreamWriter sw=new StreamWriter("存放路徑和頁面名",false,System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
}
catch
{
Response.Write ("The file could not be wirte:");
}
小結
用此方法可以方便的產生html檔案。程式使用了是迴圈替換,因此對需替換大量元素的模板速度非常快。
--------------------------------------------------------------------------------
ASP.Net產生靜態HTML頁
環境:Microsoft .NET Framework SDK v1.1
OS:Windows Server 2003 中文版
ASP.Net產生靜態HTML頁
在Asp中實現的產生靜態頁用到的FileSystemObject對象!
在.Net中涉及此類操作的是System.IO
以下是程式碼 注:此代碼非原創!參考別人代碼
Code:
//產生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); //模板頁中的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.WriteFile(this.Title.Text.ToString(),this.Content.Text.ToString(),this.Author.Text.ToString()))
{
Response.Write("添加成功");
}
else
{
Response.Write("產生HTML出錯!");
}
模板頁Text.html代碼
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ShowArticle</title>
<body>
biaoti
<br>
content<br>
author
</body>
</HTML>
biaoti
<br>
content<br>
author
</body>
</HTML>
提示添加成功後會出以目前時間為檔案名稱的html檔案!上面只是把傳遞過來的幾個參數直接寫入了HTML檔案中,在實際應用中需要先添加資料庫,然後再寫入HTML檔案
產生靜態網頁的一種方法
--------------------------------------------------------------------------------
下面代碼可以幫您產生靜態頁面,如:list.asp是讀資料庫的頁面,要生在list.htm靜態頁面,你的網域名稱是xxx.com,可以用下面代碼,使用方法:
if SaveFile("/htm/list.htm","http://www.xxx.com/asp/list.asp") then
Response.write "已產生"
else
Response.write "沒有產生"
end if
如產生失敗,請把代碼On Error Resume Next封了,查看具體錯誤資訊
代碼如下:
<%
if SaveFile("/htm/list.htm",http://www.xxx.com/asp/list.asp) then
Response.write "已產生"
else
Response.write "沒有產生"
end if
function SaveFile(LocalFileName,RemoteFileUrl)
Dim Ads, Retrieval, GetRemoteData
On Error Resume Next
Set Retrieval = Server.CreateObject("Microso" & "ft.XM" & "LHTTP")
With Retrieval
.Open "Get", RemoteFileUrl, False, "", ""
.Send
GetRemoteData = .ResponseBody
End With
Set Retrieval = Nothing
Set Ads = Server.CreateObject("Ado" & "db.Str" & "eam")
With Ads
.Type = 1
.Open
.Write GetRemoteData
.SaveToFile Server.MapPath(LocalFileName), 2
.Cancel()
.Close()
End With
Set Ads=nothing
if err <> 0 then
SaveFile = false
err.clear
else
SaveFile = true
end if
End function
%>