• ASP.NET生成静态页面


          ASP.NET应用程序中,为了提交程序的性能,往往会采用生成静态页面。生成静态页面的方法主要有
    模版替换、HttpWebRequest生成,Rander方式。这次主要说说后两种。HttpWebRequest方式Rander方式差不多,都是生成HTML文件到硬盘。差别在于如是验证方式是Form的话,只能使用Rander方式。对应于动静结合页面,一般采用两个页面,一个页面是目标页面,用来生成,另一个页面用于加载生成的文件。

    HttpWebRequest方式

     1       /// <summary>
     2         /// Write the HTML file.
     3         /// </summary>
     4         /// <param name="PageUrl">The page URL.</param>
     5         /// <param name="EncodeTypeStr">The encode type STR.</param>
     6         /// <param name="LocalDiskPath">The local disk path.</param>
     7         /// <remarks>Author:Petter Liu  http://wintersun.cnblogs.com </remarks>
     8         /// <example>
     9         /// <code>
    10         /// <![CDATA[
    11         /// WriterHtmlFile("/TDD2005/Rpt_TodayFocus.aspx", "utf-8", "/TDD2005/Rpt_TodayFocus.htm")
    12         /// ]]>
    13         /// </code>
    14         /// </example>
    15         /// <returns>Is success</returns>
    16         public static bool WriteHtmlFile(string PageUrl, string EncodeTypeStr, string LocalDiskPath)
    17         {
    18             try
    19             {
    20                 File.Delete(HttpContext.Current.Server.MapPath(LocalDiskPath));
    21                 string text = "<!--#include virtual=\"hehe\"-->";
    22                 
    23                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + HttpContext.Current.Request.ServerVariables["SERVER_NAME"+ PageUrl);
    24                 request.Timeout = 0xc350;
    25                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    26                 Encoding encoding = Encoding.GetEncoding(EncodeTypeStr);
    27                 StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
    28                 text = reader.ReadToEnd();
    29                 response.Close();
    30                 reader.Close();
    31                 StreamWriter writer = new StreamWriter(HttpContext.Current.Server.MapPath(LocalDiskPath), true, Encoding.GetEncoding(EncodeTypeStr));
    32                 writer.WriteLine(text);
    33                 writer.Close();
    34                 return true;
    35             }
    36             catch(Exception ex)
    37             {
    38                 Utility.Error.LogSql("生成文件出错", ex.ToString());
    39                 return false;
    40             }
    41         } 

    Rander方式

     1    /// <summary>
     2         /// Initializes the <see cref="T:System.Web.UI.HtmlTextWriter"/> object and calls on the child controls of the <see cref="T:System.Web.UI.Page"/> to render.
     3         /// </summary>
     4         /// <remarks>Author:Petter Liu  http://wintersun.cnblogs.com </remarks>
     5         /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> that receives the page content.</param>
     6         protected override void Render(HtmlTextWriter writer)
     7         {
     8             string filename = string.Format("z{0}.ascx", _city_id);
     9             if (File.Exists(Server.MapPath(filename)))
    10             {
    11                 File.Delete(Server.MapPath(filename));
    12             }
    13             System.IO.StringWriter html = new System.IO.StringWriter();
    14             System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html);
    15             base.Render(tw);
    16             System.IO.StreamWriter sw;
    17             sw = new System.IO.StreamWriter(Server.MapPath(filename), false, System.Text.Encoding.Default);
    18             sw.Write(html.ToString());
    19             sw.Close();
    20             tw.Close();
    21             //Response.Write(html.ToString());  //呈现页面
    22             //Response.Redirect(filename);      //重定向生成的页面
    23             Response.Redirect("index.aspx");
    24         } 

    在启动页面Page_Load

     1         /// <summary>
     2         /// Loads the static page.
     3         /// <remarks>Author:Petter Liu  http://wintersun.cnblogs.com </remarks>
     4         /// </summary>
     5         private void LoadStaticPage()
     6         {
     7             string ascxname = string.Format("z{0}.ascx"this.City_Id);
     8             if (File.Exists(Server.MapPath(ascxname)))
     9             {
    10                 System.Web.UI.Control NameControl = Page.LoadControl(ascxname);
    11                 NameControl.ID = "mainpage";
    12                 PlaceHolder1.Controls.Add(NameControl);
    13             }
    14             else
    15             {
    16                 Response.Redirect("MainPage.aspx");
    17             }
    18         } 


     

  • 相关阅读:
    CyclicBarrier与CountDownLatch区别
    导入搜狗实验室新闻语料库
    安装ik分词插件
    分页显示时传递页码的方法
    elasticsearch安装步骤
    linux查看端口占用情况
    Python:文件的读取、创建、追加、删除、清空
    R语言-选择样本数量
    不符合正态分布的配对数据也有自己的统计方法。
    python时间处理
  • 原文地址:https://www.cnblogs.com/wintersun/p/911998.html
Copyright © 2020-2023  润新知