分享

ASP.NET生成HTML静态页的一个类

 wangn 2010-09-16
 
C#代码
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Web;   
  5. using System.Net;   
  6. using System.IO;   
  7. using System.Text;   
  8. using System.Web.UI.HtmlControls;   
  9. using System.Text.RegularExpressions;   
  10.   
  11. /// <summary>   
  12. ///CreateHtml 的摘要说明   
  13. /// </summary>   
  14. public class CreateHtml:System.Web.UI.Page   
  15. {   
  16.     public CreateHtml()   
  17.      {   
  18.         //   
  19.         //TODO: 在此处添加构造函数逻辑   
  20.         //   
  21.      }   
  22.     /// <summary>   
  23.     /// 生成静态页面,生成位置是本项目下   
  24.     /// </summary>   
  25.     /// <param name="strURL">请求的URL</param>   
  26.     /// <param name="strRelPath">创建的路径及文件名,路径为相对路径</param>   
  27.     public bool Nei_Create(string strURL, string strRelPath)   
  28.      {   
  29.         string   strFilePage;   
  30.          
  31.          strFilePage = HttpContext.Current.Server.MapPath(strRelPath);   
  32.          StreamWriter sw = null;   
  33.         //获得aspx的静态html   
  34.         try  
  35.          {   
  36.                
  37.             if (File.Exists(strFilePage))   
  38.              {   
  39.                  File.Delete(strFilePage);   
  40.              }   
  41.              sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("gb2312"));   
  42.              System.Net.WebRequest wReq = System.Net.WebRequest.Create(strURL);   
  43.              System.Net.WebResponse wResp = wReq.GetResponse();   
  44.              System.IO.Stream respStream = wResp.GetResponseStream();   
  45.              System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));   
  46.             string strTemp = reader.ReadToEnd();   
  47.   
  48.              Regex r1 = new Regex("<input type=\"hidden\" name=\"__EVENTTARGET\".*/>", RegexOptions.IgnoreCase);   
  49.              Regex r2 = new Regex("<input type=\"hidden\" name=\"__EVENTARGUMENT\".*/>", RegexOptions.IgnoreCase);   
  50.              Regex r3 = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\".*/>", RegexOptions.IgnoreCase);   
  51.   
  52.              Regex r4 = new Regex("<form .*id=\"form1\">", RegexOptions.IgnoreCase);   
  53.              Regex r5 = new Regex("</form>");   
  54.   
  55.              Regex r6 = new Regex("<input type=\"hidden\" name=\"__EVENTVALIDATION\".*/>", RegexOptions.IgnoreCase);   
  56.              strTemp = r1.Replace(strTemp, "");   
  57.              strTemp = r2.Replace(strTemp, "");   
  58.              strTemp = r3.Replace(strTemp, "");   
  59.              strTemp = r4.Replace(strTemp, "");   
  60.              strTemp = r5.Replace(strTemp, "");   
  61.              strTemp = r6.Replace(strTemp, "");   
  62.   
  63.              sw.Write(strTemp);   
  64.          }   
  65.         catch (Exception ex)   
  66.          {   
  67.              HttpContext.Current.Response.Write(ex.Message);   
  68.              HttpContext.Current.Response.End();   
  69.             return false;//生成到出错   
  70.          }   
  71.         finally  
  72.          {   
  73.              sw.Flush();   
  74.              sw.Close();   
  75.              sw = null;   
  76.          }   
  77.   
  78.         return true;   
  79.      }   
  80.     /// <summary>   
  81.     /// 生成静态页面,生成位置不在本项目下   
  82.     /// </summary>   
  83.     /// <param name="strURL">请求的URL</param>   
  84.     /// <param name="strRelPath">创建的路径及文件名,路径为绝对路径</param>   
  85.     public bool Wai_Create(string strURL, string strRelPath,string filename)   
  86.      {   
  87.         string strFilePage;   
  88.          strFilePage = strRelPath + "\\" + filename;   
  89.          StreamWriter sw = null;   
  90.         //获得aspx的静态html   
  91.         try  
  92.          {   
  93.             if (!Directory.Exists(strRelPath))   
  94.              {   
  95.                  Directory.CreateDirectory(strRelPath);   
  96.              }   
  97.             if (File.Exists(strFilePage))   
  98.              {   
  99.                  File.Delete(strFilePage);   
  100.              }   
  101.              sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding("gb2312"));   
  102.              System.Net.WebRequest wReq = System.Net.WebRequest.Create(strURL);   
  103.              System.Net.WebResponse wResp = wReq.GetResponse();   
  104.              System.IO.Stream respStream = wResp.GetResponseStream();   
  105.              System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));   
  106.             string strTemp = reader.ReadToEnd();   
  107.   
  108.              Regex r1 = new Regex("<input type=\"hidden\" name=\"__EVENTTARGET\".*/>", RegexOptions.IgnoreCase);   
  109.              Regex r2 = new Regex("<input type=\"hidden\" name=\"__EVENTARGUMENT\".*/>", RegexOptions.IgnoreCase);   
  110.              Regex r3 = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\".*/>", RegexOptions.IgnoreCase);   
  111.   
  112.              Regex r4 = new Regex("<form .*id=\"form1\">", RegexOptions.IgnoreCase);   
  113.              Regex r5 = new Regex("</form>");   
  114.   
  115.              Regex r6 = new Regex("<input type=\"hidden\" name=\"__EVENTVALIDATION\".*/>", RegexOptions.IgnoreCase);   
  116.              strTemp = r1.Replace(strTemp, "");   
  117.              strTemp = r2.Replace(strTemp, "");   
  118.              strTemp = r3.Replace(strTemp, "");   
  119.              strTemp = r4.Replace(strTemp, "");   
  120.              strTemp = r5.Replace(strTemp, "");   
  121.              strTemp = r6.Replace(strTemp, "");   
  122.   
  123.              sw.Write(strTemp);   
  124.          }   
  125.         catch (Exception ex)   
  126.          {   
  127.              HttpContext.Current.Response.Write(ex.Message);   
  128.              HttpContext.Current.Response.End();   
  129.             return false;//生成到出错   
  130.          }   
  131.         finally  
  132.          {   
  133.              sw.Flush();   
  134.              sw.Close();   
  135.              sw = null;   
  136.          }   
  137.   
  138.         return true;   
  139.   
  140.      }   
  141.     public void FilePicDelete(string path)   
  142.      {   
  143.          System.IO.FileInfo file = new System.IO.FileInfo(path);   
  144.         if (file.Exists)   
  145.              file.Delete();   
  146.      }   
  147. }  
  148. 用法:
    C#代码
    1. new CreateHtml().Nei_Create("http://localhost:4032/new5mdn/default.aspx", "default.htm");  
    2. 最近学院网站要改版,于是有了这个想法,将首页生成静态文件,以提高访问速度。初步想法如下(只适应于首页静态文件生成):
      IIS中设置默认文档顺序为index.html,default.aspx
      然后重写default.aspx的render方法


      protected override void Render(HtmlTextWriter writer)  
      { //这是默认的Render实现,直接输出到writer中
      //base.Render(writer);
      StringWriter html = new StringWriter();
      HtmlTextWriter tw = new HtmlTextWriter(html);
      //通过base.Render()方法,把生成的HTML写入到我们定义的tw中
      base.Render(tw);
      string path = Server.MapPath("index.html");
      StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.Default);
      //写入到文件中
      sw.Write(html.ToString());
      sw.Close();
      //写入到writer中,以输出页面的HTML
      //注意,这里不能再调用base.Render(writer),会出现错误
      writer.Write(html.ToString());
      }



      这样在每次访问default.aspx的时候会在同目录下生成index.html文件,而一般用户访问时都会直接输入域名,如http:// 这样由于iis中的设置,用户默认访问到的都是index.html静态页面,应该能有效提高首页打开速度。因为首页更新不会太快,添加了新闻或其它在首页显示的内容的时候可以手动访问一下http:///default.aspx 这个页来重新生成index.html,或者在发布新闻后自动访问一下default.aspx页,这样就能做到index.html页内容及时更新,这种方法应该比outputcache更好,问题也会少。当然前提是首页没有各种postback事件,有登录框倒好办,直接把登录框改为常规的hmtl form,用post的方式post到登录页。

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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多