生成静态网页

方式1(模版):
   在模版中编辑一些符号,然后从数据库中提取数据替换这些符号并按照时间(年-月-日)或其他的规则生成到硬盘中.这种方式过于简单在此不在累述.

方式2(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></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         } 
方式3(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> </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

在启动页面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         } 
posted @ 2007-09-30 17:00  李占卫  阅读(333)  评论(0编辑  收藏  举报