日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ASP.NET實現(xiàn)靜態(tài)頁面方法淺析

ASP.NET實現(xiàn)靜態(tài)頁面的方法是什么呢?首先讓我們看看ASP.NET的源碼實例:

成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、做網(wǎng)站與策劃設(shè)計,周至網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:周至等地區(qū)。周至做網(wǎng)站價格咨詢:18980820575

 
 
 
  1. ﹤!--Main.Aspx--﹥   
  2. ﹤%@ page language="C#" %﹥   
  3. ﹤%@ import namespace=System.IO %﹥   
  4. ﹤script runat="server"﹥   
  5. protected override void OnInit (EventArgs e)   
  6. {   
  7.   int id;   
  8.   try   
  9.   {   
  10.   id = int.Parse (Request.QueryString["id"]);   
  11.   }   
  12.   catch   
  13.   {   
  14.   throw (new Exception ("頁面沒有指定id"));   
  15.   }   
  16.     
  17.   string filename=Server.MapPath("statichtml_"+id+".html");   
  18.     
  19.   //嘗試讀取已有文件   
  20.   Stream s = GetFileStream (filename);   
  21.   if (s != null)//如果文件存在并且讀取成功   
  22.   {   
  23.   using (s)   
  24.   {   
  25.   Stream2Stream (s, Response.OutputStream);   
  26.   Response.End ();   
  27.   }   
  28.   }   
  29.     
  30.     
  31.   //調(diào)用Main_Execute,并且獲取其輸出   
  32.   StringWriter sw = new StringWriter ();   
  33.   Server.Execute ("Main_Execute.aspx", sw);   
  34.     
  35.   string content = sw.ToString ();   
  36.     
  37.   //輸出到客戶端   
  38.   Response.Write(content);   
  39.   Response.Flush();   
  40.     
  41.   //寫進(jìn)文件   
  42.     
  43.   try   
  44.   {   
  45.   using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Write))   
  46.   {   
  47.   using (StreamWriter streamwriter = new StreamWriter (fs, Response.ContentEncoding))   
  48.   {   
  49.   streamwriter.Write (content);   
  50.   }   
  51.   }   
  52.   }   
  53.   finally   
  54.   {   
  55.   //Response.End ();   
  56.   }   
  57. }   
  58. static public void Stream2Stream (Stream src, Stream dst)   
  59. {   
  60.   byte[] buf = new byte[4096];   
  61.   while (true)   
  62.   {   
  63.   int c = src.Read (buf, 0, buf.Length);   
  64.   if(c==0)   
  65.   return;   
  66.   dst.Write (buf, 0, c);   
  67.   }   
  68. }   
  69. public Stream GetFileStream(string filename)   
  70. {   
  71.   try   
  72.   {   
  73.   DateTime dt = File.GetLastWriteTime (filename);   
  74.   TimeSpan ts=dt - DateTime.Now;   
  75.   if(ts.TotalHours﹥1)   
  76.   return null;  //1小時后過期   
  77.   return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read);   
  78.   }   
  79.   catch   
  80.   {   
  81.   return null;   
  82.   }   
  83. }   
  84. ﹤/script﹥   
  85.  
  86. ﹤!--Main_Execute.aspx--﹥   
  87. ﹤%@ page language="C#" %﹥   
  88. ﹤html﹥   
  89. ﹤head runat="server"﹥   
  90.   ﹤title﹥Untitled Page﹤/title﹥   
  91. ﹤/head﹥   
  92. ﹤body﹥   
  93. ID:   
  94. ﹤%=Request.QueryString["id"]%﹥   
  95. ﹤/body﹥   
  96. ﹤/html﹥  

其中ASP.NET實現(xiàn)靜態(tài)頁面的原理是這樣的。

Main_Execute.aspx是生成HTML的頁面。

現(xiàn)在用Main.aspx來對它進(jìn)行緩存.

ASP.NET實現(xiàn)靜態(tài)頁面過程如下:

首先根據(jù)頁面參數(shù)算出文件名。(這個例子只根據(jù)Request.QueryString["id"]來算)

嘗試讀取緩存的文件.如果成功,那么Response.End();

如果不成功:

使用Server.Execute來調(diào)用Main_Execute.aspx,并且獲取它的結(jié)果內(nèi)容。

得到內(nèi)容后,立刻輸出到客戶端。

***把內(nèi)容寫進(jìn)文件里,提供給下一次做為緩存度取。

ASP.NET實現(xiàn)靜態(tài)頁面的方法就向你介紹到這里,那么關(guān)于ASP.NET實現(xiàn)靜態(tài)頁面你是不是多了份了解了呢?

【編輯推薦】

  1. ASP.NET Session丟失問題原因及解決方案
  2. ASP.NET的代碼隱藏文件
  3. ASP.NET基礎(chǔ)教程之個性化特點淺析
  4. ASP.NET HTML控件學(xué)習(xí)的淺析
  5. ASP.NET獲取MAC地址與IP地址的程序淺析

文章名稱:ASP.NET實現(xiàn)靜態(tài)頁面方法淺析
網(wǎng)站網(wǎng)址:http://m.5511xx.com/article/cocdieo.html