国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

動態(tài)網(wǎng)頁變靜態(tài):ASP.NET生成靜態(tài)HTML頁

2024-07-10 13:08:23
字體:
供稿:網(wǎng)友

環(huán)境:microsoft .net framework sdk v1.1
os:windows server 2003 中文版
asp.net生成靜態(tài)html頁
在asp中實現(xiàn)的生成靜態(tài)頁用到的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";
// 替換內(nèi)容
// 這時,模板文件已經(jīng)讀入到名稱為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;

此函數(shù)放在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>

提示添加成功后會出以當(dāng)前時間為文件名的html文件!上面只是把傳遞過來的幾個參數(shù)直接寫入了html文件中,在實際應(yīng)用中需要先添加數(shù)據(jù)庫,然后再寫入html文件

下面是asp生成靜態(tài)網(wǎng)頁的一種方法

--------------------------------------------------------------------------------

下面代碼可以幫您生成靜態(tài)頁面,如:list.asp是讀數(shù)據(jù)庫的頁面,要生在list.htm靜態(tài)頁面,你的域名是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
%>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宾阳县| 柘荣县| 增城市| 南涧| 佛学| 那坡县| 盐边县| 江西省| 大连市| 隆化县| 泰来县| 清远市| 潍坊市| 平南县| 邵东县| 吉林省| 常州市| 盐山县| 太谷县| 普格县| 通化市| 蒙城县| 淮北市| 九江市| 洛宁县| 泉州市| 萝北县| 绥化市| 敖汉旗| 房山区| 清远市| 商南县| 穆棱市| 大埔县| 凌云县| 巢湖市| 鹿泉市| 元江| 千阳县| 博客| 黄平县|