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 }
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 }
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 }
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 }