Brief Introduction to three methods for generating HTML in ASP. NET 2.0

Source: Internet
Author: User

The database-driven dynamic web page frees our programmers from the cumbersome search replacement, but there are advantages and disadvantages in everything, reducing our work, however, the load on the server is increased, so various static page generation methods are generated. This article briefly describes and summarizes several ASP. NET static page generation technologies that I know. If there are any mistakes, please include them and give us some advice.

  • Download the code in this article

1. webrequest

Let's take a look at msdn's explanation of system. net. webrequest.

Webrequest is the abstract base class of the Request/Response Model of. NET Framework. It is used to access Internet data. Applications that use this request/response model can request data from the Internet in an unknown way. In this way, applications process webrequest instances, the specific sub-classes of the Protocol execute the specific details of the request.

Requests are sent from an application to a specific URI, such as a webpage on a server. Uri identifies the appropriate subclass to be created from a list of webrequest child instances registered for the application. Registering a webrequest child is usually used to process a specific protocol (such as HTTP or FTP), but it can also be registered to process requests to paths on a specific server or server.

If an error occurs when accessing Internet resources, the webrequest class will cause webexception. The webexception. Status attribute is one of the values of webexceptionstatus, which indicates the error source. When webexception. Status is webexceptionstatus. protocolerror, the response attribute contains the webresponse received from Internet resources.

Because the webrequest class is an abstract class, the actual behavior of the webrequest instance during runtime is determined by the subclass returned by the system. net. webrequest. Create method. For more information about default values and exceptions, see the documentation on subclasses, such as httpwebrequest and filewebrequest.

When we pass inhttp://Orhttps://, Then create returns the httpwebrequest object.file://, The create method returns the filewebrequest object .. Net Framework includeshttp://,https://Andfile://Uri scheme. Use the registerprefix method to register the custom webrequest subclass for processing other requests.

After learning about the webrequest class, the rest is simple. See the following code:

// Request a urlsystem. net. webrequest request = system. net. webrequest. create (URL); // returns the request webresponse response = request. getresponse (); // put the obtained content into streamreader. streamreader sr = new streamreader (response. getresponsestream (); streamwriter Sw = new streamwriter (path, false, system. text. encoding. default); // write to the file SW. write (Sr. readtoend (); Sw. close (); Sr. close ();

Feature: Because the URL is input by ourselves, we can crawl the pages of any program and any website. You can create a general background page or win form program to generate HTML or thief programs in batches.

Ii. response. Filter

Let's take a look at the explanation on msdn.

Response. filter: gets or sets a packaging filter object, which is used to modify the HTTP entity before transmission. When you create a stream object and set the filter attribute to a stream object, all HTTP outputs sent by write are filtered.

As the name suggests, filter is a filter that allows us to perform some additional operations before code output. First, we need to customize a class that inherits stream. The Code is as follows:

Public class responsefilter: stream {private stream m_sink; private long m_position; private filestream FS; Public responsefilter (Stream sink, string filepath) {m_sink = sink; // create a file stream FS = new filestream (filepath, filemode. openorcreate, fileaccess. write);} public override void write (byte [] buffer, int offset, int count) {// output the content to browser m_sink.write (buffer, 0, count ); // output the content to the file FS. write (buffer, 0, count) ;}// rewrite the abstract method public override bool Canread {get {return true ;}} public override bool canseek {get {return false ;}} public override bool canwrite {get {return false ;}} public override long length {get {return 0 ;}} public override long position {get {return m_position ;} set {m_position = value;} public override long seek (long offset, system. io. seekorigin direction) {return 0;} public override void setlength (long length) {m_sink.setlength (length);} // do not forget FS. close () Public override void close () {m_sink.close (); FS. close () ;}public override void flush () {m_sink.flush () ;}public override int read (byte [] buffer, int offset, int count) {return m_sink.read (buffer, offset, count );}}

Then we can add this filter to the page where HTML is to be generated.

// Note: only response. filter can be passed in to generate the HTML stream response. Filter = new responsefilter (response. filter, PATH );

Features: HTML can be generated only when the first page is browsed, so as to be generated as needed, which is more efficient. If you need to load this filter on a large page, you can use ihttphandlerfactory or ihttpmodule.

3. Page. Render

This method is also relatively simple. We rewrite the page render method to save HTML. The Code is as follows:

Protected override void render (htmltextwriter writer) {// This is the default render implementation and is directly output to writer // base. render (writer); stringwriter html = new stringwriter (); htmltextwriter Tw = new htmltextwriter (HTML); // use base. the render () method writes the generated HTML to the TW base we define. render (TW); string Path = server. mappath ("~ /Html/render _ "+ guid. newguid () + ". html "); streamwriter Sw = new streamwriter (path, false, system. text. encoding. default); // write to SW in the file. write (HTML. tostring (); Sw. close (); // write to writer to output the HTML of the page // note that base cannot be called here. render (writer), the error writer will appear. write (HTML. tostring ());}

Features: complicated. :-(

Iv. Summary

To sum up, generating HTML in ASP. NET is simple and convenient. We recommend that you use the first and second types. There are many methods to replace custom templates with text, but they are also troublesome, so we will not discuss them here.

This article only demonstrates some basic methods for generating HTML, and does not involve various generating policies and URL rewriting schemes in specific use. We will discuss these methods later.

Issues to be resolved:

  • How to handle viewstate when browsing the generated HTML page (discard or ......)
  • How to handle page PostBack (no PostBack or ......)

References:

  • Msdn
  • How to freeze a dynamic ASPX page into a static html page (on the server)
  • [2007 last blog] URL address rewriting, using httphander to manually compile the page and generate static html files as needed

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.