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

首頁 > 編程 > .NET > 正文

ASP.Net/asp 生成靜態HTML頁

2024-07-10 13:11:47
字體:
來源:轉載
供稿:網友

環境: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"

  onmouseup=" onmouseup=change1('a"&rs("id")&"','b"&rs("id")&"');"

 else

  menutype="menu1"

  listtype="list"

  onmouseup=" onmouseup=change2('a"&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='小灰,[email protected]'>")

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 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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麻阳| 静安区| 松江区| 涞源县| 香港| 钟山县| 双峰县| 绥中县| 安龙县| 孟州市| 盖州市| 灵川县| 桂林市| 罗平县| 南开区| 柳河县| 九台市| 金秀| 黎平县| 仁寿县| 孟州市| 侯马市| 宣化县| 天峨县| 长治县| 安康市| 济南市| 新绛县| 扎囊县| 嘉兴市| 玛沁县| 闽清县| 屏山县| 什邡市| 墨脱县| 云浮市| 涡阳县| 方山县| 揭西县| 隆化县| 湛江市|