日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

ASP.Net生成静态HTML页

發布時間:2025/7/25 HTML 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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
%>

轉載于:https://www.cnblogs.com/zhuor/archive/2006/04/04/366769.html

總結

以上是生活随笔為你收集整理的ASP.Net生成静态HTML页的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。