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

Asp.Net動態(tài)頁面轉(zhuǎn)換

開發(fā) 后端
本文介紹Asp.Net動態(tài)頁面轉(zhuǎn)靜態(tài)頁面的方法網(wǎng)上比較多,我在網(wǎng)上找了一些源代碼,并作修改?,F(xiàn)在把修改后的代碼以及說明寫一下。

關(guān)于在Asp.Net動態(tài)頁面轉(zhuǎn)靜態(tài)頁面的方法網(wǎng)上比較多。結(jié)合實際的需求,我在網(wǎng)上找了一些源代碼,并作修改。現(xiàn)在把修改后的代碼以及說明寫一下。

一個Asp.Net動態(tài)頁面轉(zhuǎn)換的類,該類通過靜態(tài)函數(shù)Changfile()來實現(xiàn),Asp.Net動態(tài)頁面到靜態(tài)頁面的轉(zhuǎn)換。

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Text;  
  11. using System.IO;  
  12. /**////  
  13. /// Summary description for HtmlProxy  
  14. ///  
  15. public class HtmlProxy  
  16. ...{  
  17. public HtmlProxy()  
  18. ...{  
  19. }  
  20. public static bool ChangeFile(int id)  
  21. ...{  
  22. string filename = HttpContext.Current.Server.MapPath("Post_" + id + ".html");  
  23. //嘗試讀取已有文件 Stream st = GetFileStream(filename);  
  24. //如果文件存在并且讀取成功  
  25. if (st != null)  
  26. ...{  
  27. using (st)  
  28. ...{  
  29. StreamToStream(st, HttpContext.Current.Response.OutputStream);  
  30. return true;  
  31. //Response.End();  
  32. }  
  33. }  
  34. else  
  35. ...{  
  36. StringWriter sw = new StringWriter();  
  37. HttpContext.Current.Server.Execute("ForumDetail.aspx?PID=" + id, sw);  
  38. string content = sw.ToString();  
  39. //寫進文件  
  40.  
  41. try  
  42. ...{  
  43. using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write))  
  44. ...{  
  45. using (StreamWriter stw = new StreamWriter(fs, HttpContext.Current.Response.ContentEncoding))  
  46. ...{  
  47. stw.Write(content);  
  48. }  
  49. }  
  50. return true;  
  51. }  
  52. catch ...{ return false; }  
  53. }  
  54. }  
  55. private static Stream GetFileStream(string filename)  
  56. ...{  
  57. try  
  58. ...{  
  59. DateTime dt = File.GetLastWriteTime(filename);  
  60. TimeSpan ts = dt - DateTime.Now;  
  61. if (ts.TotalHours >1)  
  62. ...{  
  63. //一小時后過期  
  64. return null;  
  65. }  
  66. return new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);  
  67. }  
  68. catch ...{ return null; }  
  69. }  
  70. static public void StreamToStream(Stream src, Stream dst)  
  71. ...{  
  72. byte[] buf = new byte[4096];  
  73. while (true)  
  74. ...{  
  75. int c = src.Read(buf, 0, buf.Length);  
  76. if (c == 0)  
  77. return;  
  78. dst.Write(buf, 0, c);  
  79. }  
  80. }  
  81. }  
  82. 在頁面文件中,F(xiàn)orURL.aspx的后臺代碼如下:  
  83. protected void Page_Load(object sender, EventArgs e)  
  84. ...{  
  85. try  
  86. ...{  
  87. int id = int.Parse(Request.QueryString["PID"]);  
  88. if(HtmlProxy.ChangeFile(id))  
  89. ...{  
  90. Response.Redirect("Post_" + id + ".html");  
  91. }  
  92. else  
  93. ...{  
  94. Response.Redirect("Post.aspx?PID=" + id );  
  95. }  
  96. }  
  97. catch ...{  
  98. }  

  

【編輯推薦】

  1. 淺談ASP.NET應(yīng)用程序
  2. ASP.NET的預(yù)編譯應(yīng)用程序
  3. 概述ASP.NET 2.0的FormView控件
  4. 優(yōu)化ASP.NET 2.0 Profile Provider
  5. 淺析ASP.NET進程模型配置
責任編輯:佚名 來源: 51CTO.com
相關(guān)推薦

2009-07-29 17:26:39

ASP.NET頁面

2009-07-23 14:17:41

2009-07-31 10:23:44

緩存頁面ASP.NET緩存

2009-08-03 13:38:18

ASP.NET編程模型

2009-07-29 14:35:34

頁面輸出緩存ASP.NET

2009-07-23 10:52:38

2009-07-27 15:25:40

aspx頁面ASP.NET

2009-07-31 10:33:54

ASP.NET頁面輸出

2009-08-05 18:22:55

2009-07-23 14:21:55

ASP.NET頁面

2009-07-28 16:40:11

ASP.NET異步頁面

2009-08-04 18:10:35

ASP.NET動態(tài)編譯

2009-07-23 14:08:58

2009-07-31 13:06:53

CheckBoxLisASP.NET頁面

2009-07-29 16:41:45

ASP.NET頁面框架

2009-08-05 14:01:50

ASP.NET配置錯誤

2009-08-04 15:58:06

ASP.NET動態(tài)控件

2009-07-28 10:01:16

ASP.NET Exc

2009-08-04 18:05:37

動態(tài)編譯ASP.NET

2009-08-03 14:18:40

ASP.NET編程模型ASP.NET頁面生命
點贊
收藏

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