asp.net 2.0 output Text format stream

Source: Internet
Author: User

When you are programming with ASP.net, opening a page typically invokes the specified paging file (. html,. aspx), and so on, by specifying a hyperlink address.

However, if the content of the page file that is to be opened is dynamically generated in the program, or is taken out from the table in the database, how do we show the content?

Our most direct idea is to save the content as a Web page file and then call it. This approach is certainly possible, but not the best approach, because it generates on the WEB server

Many temporary files, these files may never be unnecessary.

Another best approach is to use the text format stream to dynamically display the content of the page. For example, there is a page:

  ……
  <iFrame src=""></iframe>
  ……

You need to open a page with an iFrame, and the content of the page is dynamically generated. We can write a. ashx file (named Html.ashx here) to process the IHttpHandler interface class in the. ashx file, which can directly generate the data format used by the browser.

Html.ashx File Contents:

Using System;
Using System.IO;
Using System.Web;
public class Handler:ihttphandler {
public bool IsReusable {
get {
return true;
}
}
public void ProcessRequest (HttpContext context)
{
Set up the response settings
Context. Response.ContentType = "text/html";
Context. Response.Cache.SetCacheability (Httpcacheability.public);
Context. Response.bufferoutput = false;
Stream stream = null;
String html = "Success: test of Txt.ashx";
byte[] html2bytes = System.Text.Encoding.ASCII.GetBytes (HTML);
stream = new MemoryStream (html2bytes);
if (stream = = null)
stream = new MemoryStream (System.Text.Encoding.ASCII.GetBytes ("Get nothing!"));
Write text stream to the response stream
const int buffersize = 1024 * 16;
byte[] buffer = new Byte[buffersize];
int count = stream. Read (buffer, 0, buffersize);
while (Count > 0)
{
Context. Response.OutputStream.Write (buffer, 0, count);
Count = stream. Read (buffer, 0, buffersize);
}
}
}

The Html.ashx file first converts a string string into an array of bytes (byte), and then generates the MemoryStream data stream in memory, which is finally written to the OutputStream object and is displayed.

In this way, we can use the <iframe src= "Html.ashx" ></iframe> to show dynamically generated pages, showing "success: test of Txt.ashx" page content. String html = "Success: test of Txt.ashx" in Html.ashx file; In the sentence, the contents of the variable HTML can be completely obtained from the database (the contents of an HTML file are stored in the database in advance).

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.