自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
ASP.NET實現(xiàn)靜態(tài)頁面的方法是什么呢?那么本文就向你介紹ASP.NET實現(xiàn)靜態(tài)頁面的具體操作以及實例分析。

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

  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.   //寫進文件   
  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來對它進行緩存.

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)容寫進文件里,提供給下一次做為緩存度取。

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地址的程序淺析
責(zé)任編輯:仲衡 來源: CSDN博客
相關(guān)推薦

2009-07-28 16:40:11

ASP.NET異步頁面

2009-08-03 13:38:18

ASP.NET編程模型

2009-07-29 16:41:45

ASP.NET頁面框架

2009-08-05 14:01:50

ASP.NET配置錯誤

2009-08-04 17:28:45

Apache支持ASP

2009-06-24 09:12:26

ASP.NET頁面請求

2009-08-05 14:17:27

ASP.NET錯誤頁面

2009-07-28 10:01:16

ASP.NET Exc

2009-07-31 12:43:59

ASP.NET MVC

2009-08-05 15:50:13

ASP.NET優(yōu)點

2009-07-20 16:09:39

2009-08-14 13:37:25

ASP.NET靜態(tài)頁面

2009-10-10 10:44:15

ASP.NET頁面緩存

2009-07-24 13:41:15

ASP.NET AJA

2009-08-05 18:36:12

ASP.NET Che

2009-08-04 10:02:36

中國站長站

2009-08-10 13:32:15

ASP.NET TimASP.NET組件設(shè)計

2009-08-05 16:59:55

ASP.NET組件設(shè)計

2009-07-27 09:29:38

ASP.NET中Jav

2009-07-20 16:23:01

ASP.NET授權(quán)模塊
點贊
收藏

51CTO技術(shù)棧公眾號