asp.net two ways to generate HTM static files

Source: Internet
Author: User
Tags datetime flush tostring
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}

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.