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).