分享

Asp.Net动态页面转换

 轻应用开发 2014-04-16

关于在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进程模型配置
【责任编辑:志京 TEL:(010)68476606】

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多