av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

ASP.NET實現靜態(tài)頁面方法淺析

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

成都創(chuàng)新互聯是一家專注于成都網站建設、做網站與策劃設計,周至網站建設哪家好?成都創(chuàng)新互聯做網站,專注于網站建設10多年,網設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:周至等地區(qū)。周至做網站價格咨詢: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.   //調用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.   //寫進文件   
  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實現靜態(tài)頁面的原理是這樣的。

Main_Execute.aspx是生成HTML的頁面。

現在用Main.aspx來對它進行緩存.

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

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

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

如果不成功:

使用Server.Execute來調用Main_Execute.aspx,并且獲取它的結果內容。

得到內容后,立刻輸出到客戶端。

***把內容寫進文件里,提供給下一次做為緩存度取。

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

【編輯推薦】

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

網站名稱:ASP.NET實現靜態(tài)頁面方法淺析
文章URL:http://uogjgqi.cn/article/cocdieo.html
掃二維碼與項目經理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯網交流