從資料庫教程中查詢出title,content,datetime三個內容之後,分別替換掉模版中對應位置的{title},{content},{datetime}然後儲存為xx.html一個靜態頁就產生了。
今天寫的執行個體主要體現替換儲存的步驟。
模版就不做了,直接整個body作為一個標籤,把要做成靜態頁的動態網頁面的body部分當作是查詢出的資料替換掉{body}部分,產生一張靜態頁。(首頁靜態可以這麼做哦)。
1.使用serever.Excute
StreamWriter sw = new StreamWriter(Server.MapPath("html/Login.html"), false);
Server.Execute("ShowColumn.asp教程x?id=1&page=2", sw);
sw.Close();
2.替換字元
url重寫
1.定義重寫規則
urls.xml 變成urls.config
<?xml version="1.0" encoding="utf-8" ?>
<Urls>
<rewrite name="ShowArticle" pattern="article-(d+).html" path ="article-{0}.html" page="showarticle.aspx" query="id=$1"></rewrite>
<rewrite name="ShowList" pattern="list-(d+).html" path ="list-{0}.html" page="showlist.aspx" query="id=$1"></rewrite>
</Urls>
2.建立一個簡單的實體urls類
3.urls類 擷取urls.config檔案中的所有url
4.httpmodule類處理 請求的地址
5.在web.config httpmodule節點添加
方法二上面的實現原理
先讀模數版內容
#region 讀模數版
private string template(string uri) {
string content;
StreamReader sr;
sr = File.OpenText(uri);
content = sr.ReadToEnd();
return content;
}
#endregion
模版是個空頁面,因為我們主要類比這個寫入的讀模數版寫入儲存的過程。
整張template.html檔案中就一行$html$。
接著是擷取目標頁內容
#region 篩選內容
private string Content(string uri){
HtmlAgilityPack.HtmlWeb htmlWeb = new HtmlAgilityPack.HtmlWeb();
HtmlDocument hDoc = htmlWeb.Load(uri);
string content = hDoc.DocumentNode.InnerHtml;
return content;
}
#endregion
然後是將擷取的目標頁內容替換掉模版頁的$html$部分
string tpuri = txtTp.Text;
string pageuri = txtUri.Text;
string tpcontent = Template(tpuri);
string pgcontent = Content(pageuri);
string result = tpcontent.Replace(“$html$”, pgcontent);
最後寫個儲存替換好的內容的方法
#region 儲存為靜態頁
private void CreatePage(string result) {
StreamWriter sw;
sw = File.CreateText(“index.html”);
sw.Write(result);
sw.Close();
}
#endregion
最後完成組建靜態頁
string tpuri = txtTp.Text;
string pageuri = txtUri.Text;
string tpcontent = Template(tpuri);
string pgcontent = Content(pageuri);
string result = tpcontent.Replace(“$html$”, pgcontent);
CreatePage(result);
測試的時候路徑會在IDE檔案夾 整個過程就是這樣。
如果要產生大批量的頁面上面的看上去會有點問題,伺服器負載哦,你懂的。
看個產生表態頁面的類
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. }
調用方法
new CreateHtml().Nei_Create("http://localhost:4032/new5mdn/default.aspx", "default.htm");