asp.net two ways to generate HTM static files
Last Update:2017-02-28
Source: Internet
Author: User
asp.net| static//51aspx.com generate static page demo file, reprint please keep this information
2 Public partial class _default:system.web.ui.page
3.. {
4 protected void Page_Load (object sender, EventArgs e)
5.. {
6
7}
8
9//According to the template generated, remain in the HTML folder (part of the source code collection on the network)
protected void Button1_Click (object sender, EventArgs e)
11.. {
12//Source code is the replacement of the feature characters in the template
13
Mbpath =server.mappath ("template.htm");
Encoding code = encoding.getencoding ("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
-string str = NULL;
19
20//Read
Try
22.. {
sr = new StreamReader (Mbpath, code);
str = Sr. ReadToEnd ();
25
26}
catch (Exception ex)
28.. {
Throw ex;
30}
-Finally
32.. {
Sr. Close ();
34}
35
36//automatically renamed according to time, the extension can also be modified by itself
Panax Notoginseng String fileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". htm";
str = str. Replace ("$title $", txttitle.text);//Replacement title
str = str. Replace ("$content $", txtcontent.text);//replacement content
40
41//Generate static file
A try
43.. {
SW = New StreamWriter (Server.MapPath ("htm/") + FileName, false, code);
SW. Write (str);
SW. Flush ();
47
48}
catch (Exception ex)
50.. {
Wuyi throw ex;
52}
Finally
54.. {
SW. Close ();
Response.Write ("Congratulations <a href=htm/" +filename+ target=_blank> "+filename+" </a> has been generated, saved in the HTM folder! ");
57}
58
59
60}
61
62
63////Generate static page maintenance based on URL address
protected void button2_click (object sender, EventArgs e)
65.. {
Encoding code = encoding.getencoding ("Utf-8");
StreamReader sr = null;
StreamWriter sw = null;
The string str = NULL;
70
71//Read remote path
WebRequest temp = WebRequest.Create (TxtUrl.Text.Trim ());
WebResponse myTemp = temp. GetResponse ();
sr = new StreamReader (Mytemp.getresponsestream (), code);
75//Read
A try
77.. {
The SR = new StreamReader (Mytemp.getresponsestream (), code);
The is str = Sr. ReadToEnd ();
80
81}
The catch (Exception ex)
83.. {
Throw ex;
85}
Finally
87.. {
The SR. Close ();
89}
The string fileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". htm";
91
92//write
The Try
94.. {
SW = New StreamWriter (Server.MapPath ("htm/") + FileName, false, code);
SW. Write (str);
SW. Flush ();
98
99}
catch (Exception ex)
101.. {
102 Throw ex;
103}
Finally
105.. {
the SW. Close ();
Response.Write ("Congratulations <a href=htm/" + filename + "target=_blank>" + filename + "</a> has been generated, saved in HT Under the M folder! ");
108}
109
110}
111}