. Net static page generation method summary

Source: Internet
Author: User
1st methods:
The server. Execute (path as string, writer as sysetem. Io. textwriter) method is used. This method is very simple. You can open a dynamic web page request to the server to obtain the HTML of the webpage client. Code And then write the content into the file. This method is relatively simple:

1 Dim Swhtml As Stringwriter = New Stringwriter ()
2 Server. Execute ( " Http: // localhost/newsszhome/manage/newstemplate. aspx, swhtml)
3 Dim Strcontent As String = Swhtml. tostring ()
4
5 Dim Filepath As String = " D // news // 001.html "
6 If Not (System. Io. Directory. exists (system. Io. Path. getdirectoryname (filepath ))) Then
7 System. Io. Directory. createdirectory (system. Io. Path. getdirectoryname (filepath ))
8 End If
9 Dim SW As Streamwriter = New Streamwriter (filepath, False , System. Text. encoding. Default)
10 Try
11 Sw. Write (strcontent)
12 Catch Ex As Exception
13 Throw Ex
14 Finally
15 Sw. Flush ()
16 Sw. Close ()
17 End Try

In this way, you must read the webpage address. The disadvantage is obvious: the speed is slow. In addition, if the requested dynamic page has a verification control, the returned HTML page cannot perform data verification. IfProgramIt is good to use this method, but if we want to separate the generative program from the webpage program (such as WebService), this method is equivalent to opening an Internet webpage, efficiency will definitely be compromised (and I cannot use this method on WebService at all, and the program has an exception! The specific reason is not explored. It is estimated that it is a permission issue ).

2nd methods:
This method is similar to the 1st method (you also need to read the webpage content), using system. net. webrequest. create (path as string) method creates a webrequest for the webpage to be read, obtains its webresponse, and then writes the file in the form of a stream.


1 Dim Wreq As System. net. webrequest = System. net. webrequest. Create ( " Http: // localhost/newsszhome/manage/newstemplate. aspx "
2 Dim Wresp As System. net. webresponse = Wreq. getresponse
3 Dim SRS As System. Io. Stream = Wresp. getresponsestream
4 Dim Sr As System. Io. streamreader = New System. Io. streamreader (SRS, system. Text. encoding. Default) ' Getencoding ("gb2312 "))
5 Dim Strcontent As String = Sr. readtoend ()
6 Dim Filepath As String = " D: // news // 0001.html "
7 If Not (System. Io. Directory. exists (system. Io. Path. getdirectoryname (filepath ))) Then
8 System. Io. Directory. createdirectory (system. Io. Path. getdirectoryname (filepath ))
9 End If
10 Dim SW As Streamwriter = New Streamwriter (filepath, False , System. Text. encoding. Default)
11 Try
12 Sw. Write (temp)
13 Catch Ex As Exception
14 Throw Ex
15 Finally
16 Sw. Flush ()
17 Sw. Close ()
18 End Try

The effect is not much said. It is the same as the problem with the 1st methods! (However, it can still be generated successfully when I use the above method in WebService, but the speed is much slower .)

There are 3rd types, which are the most common and practical character substitution Methods string. replace (): reads a template from a file, replaces the parameters in the template, and outputs the file. This method is generated much faster than the first method, and the template content can be edited using any tool.
Main Code:
1 Dim Sr As New System. Io. streamreader ( " D: // newsdetail_template.htm " , System. Text. encoding. Default)
2 Dim Temp As String = Sr. readtoend ()
3 Temp = Temp. Replace ( " @ $ _ Createdate _ $ @ " , Datetime. Now. tostring)
4 Dim Filepath As String = " D: // news // 001.html "
5 If Not (System. Io. Directory. exists (system. Io. Path. getdirectoryname (filepath ))) Then
6 System. Io. Directory. createdirectory (system. Io. Path. getdirectoryname (filepath ))
7 End If
8 Dim SW As Streamwriter = New Streamwriter (filepath, False , System. Text. encoding. Default)
9 Try
10 Sw. Write (temp)
11 Catch
12 Return False
13 Finally
14 Sw. Flush ()
15 Sw. Close ()
16 End Try

This method reads the pure file type in the hard disk, and queries data in the background of the program to replace the specific characters in the template file.

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.