環境: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.writefilethis.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文件
新聞熱點
疑難解答
圖片精選