Asp.Net动态页面转换

开发 后端
本文介绍Asp.Net动态页面转静态页面的方法网上比较多,我在网上找了一些源代码,并作修改。现在把修改后的代码以及说明写一下。

关于在Asp.Net动态页面转静态页面的方法网上比较多。结合实际的需求,我在网上找了一些源代码,并作修改。现在把修改后的代码以及说明写一下。

一个Asp.Net动态页面转换的类,该类通过静态函数Changfile()来实现,Asp.Net动态页面到静态页面的转换。

  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. 在页面文件中,ForURL.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应用程序
  2. ASP.NET的预编译应用程序
  3. 概述ASP.NET 2.0的FormView控件
  4. 优化ASP.NET 2.0 Profile Provider
  5. 浅析ASP.NET进程模型配置
责任编辑:佚名 来源: 51CTO.com
相关推荐

2009-07-23 14:17:41

2009-07-29 17:26:39

ASP.NET页面

2009-07-31 10:23:44

缓存页面ASP.NET缓存

2009-08-03 13:38:18

ASP.NET编程模型

2009-07-31 10:33:54

ASP.NET页面输出

2009-07-29 14:35:34

页面输出缓存ASP.NET

2009-07-23 10:52:38

2009-08-05 18:22:55

2009-07-27 15:25:40

aspx页面ASP.NET

2009-07-23 14:21:55

ASP.NET页面

2009-07-28 16:40:11

ASP.NET异步页面

2009-08-04 18:10:35

ASP.NET动态编译

2009-07-31 13:06:53

CheckBoxLisASP.NET页面

2009-07-23 14:08:58

2009-07-29 16:41:45

ASP.NET页面框架

2009-08-05 14:01:50

ASP.NET配置错误

2009-08-04 18:05:37

动态编译ASP.NET

2009-07-28 10:01:16

ASP.NET Exc

2009-08-04 15:58:06

ASP.NET动态控件

2009-08-03 14:18:40

ASP.NET编程模型ASP.NET页面生命
点赞
收藏

51CTO技术栈公众号