當(dāng)我們的網(wǎng)站訪問量很大的時(shí)候,客戶端的每一次post都去大量調(diào)用數(shù)據(jù)庫(kù)服務(wù)器是一件多么可怕的事。系統(tǒng)性能會(huì)大打折扣,輕則速度很慢、數(shù)據(jù)庫(kù)鎖死,重則系統(tǒng)崩潰。本文將通過(guò)實(shí)現(xiàn)靜態(tài)html頁(yè)面解決這個(gè)問題。 
1、建立conn.cs類文件 
using system; 
//記得添加以下三引用 
using system.text; 
using system.web; 
using system.io; 
namespace myservers 
{ 
 /// <summary> 
 /// conn 的摘要說(shuō)明。 
 /// </summary> 
 public class conn 
 { 
  public conn() 
  { 
   // 
   // todo: 在此處添加構(gòu)造函數(shù)邏輯 
   // 
  } 
  public bool writefile(string strtext,string strcontent,string strauthor) 
  { 
   string path = httpcontext.current.server.mappath("/myservers/news/");//定義html文件存放路徑 
   encoding code = encoding.getencoding("gb2312");//定義文字編碼 
   // 讀取模板文件 
   string temp = httpcontext.current.server.mappath("/myservers/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=path + datetime.now.tostring("yyyymmddhhmmss")+".html"; 
   // 替換內(nèi)容 
   // 這時(shí),模板文件已經(jīng)讀入到名稱為str的變量中了 
   str = str.replace("showarticle",strtext); //模板頁(yè)中的showarticle 
   str = str.replace("title",strtext); 
   str = str.replace("content",strcontent); 
   str = str.replace("author",strauthor); 
   // 寫文件 
   try 
   { 
    sw = new streamwriter(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; 
  } 
  } 
} 
2、addnews.aspx文件 
 添加三和textbox分別為:tbx_title、tbx_content、tbx_author和一個(gè)button:btn_addnews。 
addnews.aspx.cs文件 
private void btn_addnews_click(object sender, system.eventargs e) 
  { 
   conn hover = new conn(); 
   if(hover.writefile(this.txb_title.text.tostring(),server.htmldecode(this.txb_content.value),this.txb_author.text.tostring())) 
   { 
    response.write("添加成功"); 
   } 
   else 
   { 
    response.write("生成html出錯(cuò)!"); 
   } 
  } 
3、添加模板text.html文件  
<head>showarticle</head>
<body>
title<br>
content<br>
author
</body>
說(shuō)明:news文件夾必須賦予asp.net用戶寫入的權(quán)限。這是一個(gè)簡(jiǎn)單的實(shí)現(xiàn)例子,實(shí)際項(xiàng)目必須先將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)下面,在datagird中調(diào)用數(shù)據(jù)庫(kù)下面html文件的url地址。
評(píng)論
# re: asp.net下實(shí)現(xiàn)靜態(tài)頁(yè)面(html) 2005-09-12 23:52 sunshine 
注意:默認(rèn)情況下,我們是不能向textbox、textarea中添加html語(yǔ)法的,必須修改config文件,在<system.web>下面添加<pages validaterequest="false" />,但是這樣做的話,整個(gè)項(xiàng)目中都允許鍵入html標(biāo)簽了,暫時(shí)還不知道其他的方法。 
必須使用server.htmldecode(this.content.value).tostring()對(duì)字符解碼!!!  
新聞熱點(diǎn)
疑難解答
圖片精選