Two methods for generating static web pages (Asp.net)

Source: Internet
Author: User
ASP. NETThere are two main ways to generate static pages: Template generation and URL-based generation.

Generate static files by entering the content replacement template or URL address

Templete.htm is the template file, and htm is the location where the generated static files are saved

Below is the. CS File

2 public partial class _ default: system. Web. UI. Page
3 {
4 protected void page_load (Object sender, eventargs E)
5 {
6
7}
8
9 // generated based on the template and kept in the HTML folder (some source code is collected on the Network)
10 protected void button#click (Object sender, eventargs E)
11 {
12 // The Source Code replaces the feature characters in the template.
13
14 string mbpath = server. mappath ("template.htm ");
15 encoding code = encoding. getencoding ("gb2312 ");
16 streamreader sr = NULL;
17 streamwriter Sw = NULL;
18 string STR = NULL;
19
20 // read
21 try
22 {
23 sr = new streamreader (mbpath, Code );
24 STR = Sr. readtoend ();
25
26}
27 catch (exception ex)
28 {
29 throw ex;
30}
31 finally
32 {
33 Sr. Close ();
34}
35
36 // automatically rename by time, and the extension can also be modified by yourself
37 string filename = datetime. Now. tostring ("yyyymmddhhmmss") + ". htm ";
38 STR = Str. Replace ("$ title $", txttitle. Text); // Replace the title
39 STR = Str. Replace ("$ content $", txtcontent. Text); // replace content
40
41 // generate static files
42 try
43 {
44 Sw = new streamwriter (server. mappath ("htm/") + filename, false, Code );
45 Sw. Write (STR );
46 Sw. Flush ();
47
48}
49 catch (exception ex)
50 {
51 throw ex;
52}
53 finally
54 {
55 Sw. Close ();
56 response. write ("Congratulations <a href = htm/" + filename + "target = _ blank>" + filename + "</a> generated and saved in the HTM folder! ");
57}
58
59
60}
61
62
63 // create a web page: generate static page persistence based on the URL
64 protected void button2_click (Object sender, eventargs E)
65 {
66 encoding code = encoding. getencoding ("UTF-8 ");
67 streamreader sr = NULL;
68 streamwriter Sw = NULL;
69 string STR = NULL;
70
71 // read the remote path
72 webrequest temp = webrequest. Create (txturl. Text. Trim ());
73 webresponse mytemp = temp. getresponse ();
74 sr = new streamreader (mytemp. getresponsestream (), Code );
75 // read
76 try
77 {
78 sr = new streamreader (mytemp. getresponsestream (), Code );
79 STR = Sr. readtoend ();
80
81}
82 catch (exception ex)
83 {
84 throw ex;
85}
86 finally
87 {
88 Sr. Close ();
89}
90 string filename = datetime. Now. tostring ("yyyymmddhhmmss") + ". htm ";
91
92 // write
93 try
94 {
95 Sw = new streamwriter (server. mappath ("htm/") + filename, false, Code );
96 Sw. Write (STR );
97 Sw. Flush ();
98
99}
100 catch (exception ex)
101 {
102 throw ex;
103}
104 finally
105 {
106 Sw. Close ();
107 response. write ("Congratulations <a href = htm/" + filename + "target = _ blank>" + filename + "</a> generated and saved in the HTM 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.