Asp.net: two methods for generating html pages

Source: Internet
Author: User

A static page is generated.
The instances written today mainly reflect the steps for replacement and storage.
The template will not be done. directly use the whole body as a tag, replace the body of the dynamic page to be made into a static page with the data queried, and generate a static page. (Static homepage can do this ).

1. Use serever. Excute

StreamWriter sw = new StreamWriter (Server. MapPath ("html/Login.html"), false );
Server. Execute ("ShowColumn. asp tutorial x? Id = 1 & page = 2 ", sw );
Sw. Close ();


2. Replace characters

Url rewriting
1. Define Rewrite Rules

Urls. xml is changed to urls. config
<? Xml version = "1.0" encoding = "UTF-8"?>
<Urls>
<Rewrite name = "ShowArticle" pattern = "article-(d00000000.html" path = "article-000000000.html" page = "showarticle. aspx" query = "id = $1"> </rewrite>
<Rewrite name = "ShowList" pattern = "list-(d00000000.html" path = "list-000000000.html" page = "showlist. aspx" query = "id = $1"> </rewrite>
</Urls>

2. Create a simple object urls class
3. The urls class obtains all urls in the urls. config file.
4. httpmodule class handles the request address
5. On the web. config httpmodule node, add

Method 2 implementation principle above

Read the template content first

# Region read Template
Private string template (string uri ){
String content;
StreamReader sr;
Sr = File. OpenText (uri );
Content = sr. ReadToEnd ();
Return content;
}
# Endregion

The template is an empty page, because we mainly simulate the process of writing and saving the read template.
The entire template.html file contains a line of $ html $.
The next step is to get the content of the target page.

# Region Content Filtering
Private string Content (string uri ){
HtmlAgilityPack. HtmlWeb htmlWeb = new HtmlAgilityPack. HtmlWeb ();
HtmlDocument hDoc = htmlWeb. Load (uri );
String content = hDoc. DocumentNode. InnerHtml;
Return content;
}
# Endregion
Then, replace the obtained target page with the $ html $ part of the template page.
String tpuri = txtTp. Text;
String pageuri = txtUri. Text;
String tpcontent = Template (tpuri );
String pgcontent = Content (pageuri );
String result = tpcontent. Replace ("$ html $", pgcontent );
Finally, write a method to save the replaced content.
# Save region as a static page
Private void CreatePage (string result ){
StreamWriter sw;
Sw = File.CreateText(“index.html ");
Sw. Write (result );
Sw. Close ();
}
# Endregion
Finally, the static page is generated.
String tpuri = txtTp. Text;
String pageuri = txtUri. Text;
String tpcontent = Template (tpuri );
String pgcontent = Content (pageuri );
String result = tpcontent. Replace ("$ html $", pgcontent );
CreatePage (result );


During the test, the path is stored in the IDE folder.

 


If you want to generate a large number of pages, the above may look a bit problematic, server load, you know.

View the class that generates the statement page

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. // Summary of CreateHtml
13. // </summary>
14. public class CreateHtml: System. Web. UI. Page
15 .{
16. public CreateHtml ()
17 .{
18 .//
19. // TODO: add the constructor logic here
20 .//
21 .}
22. // <summary>
23. // generate a static page
24. /// </summary>
25. /// <param name = "strURL"> request URL </param>
26. /// <param name = "strRelPath"> path and file name. The path is relative path. </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. // obtain the static html of aspx
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; // generation Error
70 .}
71. finally
72 .{
73. sw. Flush ();
74. sw. Close ();
75. sw = null;
76 .}
77.
78. return true;
79 .}
80. // <summary>
81. // generate a static page, which is not in this project
82. // </summary>
83. // <param name = "strURL"> request URL </param>
84. /// <param name = "strRelPath"> path and file name, absolute path </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. // obtain the static html of aspx
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; // generation Error
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 .}

Call Method

New CreateHtml (). Nei_Create ("http: // localhost: 4032/new5mdn/default. aspx", "default.htm ");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.