Asp.net captures the output html

Source: Internet
Author: User

After learning about the lifecycle of the Asp.net page, you may have the idea that the server can intercept the HTML to be output when HTML is output, I found a lot of information on the Internet and did not find the answer. I finally learned how to do it with the help of an expert. I 'd like to share it with you.

As we all know, in the lifecycle of a page, the last stage is rendering and executing the render event.

You can rewrite this method.

Protected override void render (htmltextwriter writer)
{
// Base. Render (writer );

System. Io. memorystream mem = new system. Io. memorystream ();
System. Io. streamwriter twr = new system. Io. streamwriter (MEM );
System. Web. UI. htmltextwriter mywriter = new htmltextwriter (twr );
Base. Render (mywriter );

mywriter. Flush ();
mywriter. Dispose ();

System. Io. streamreader strmrdr = new system. Io. streamreader (MEM );
Strmrdr. basestream. Position = 0;
String pagecontent = strmrdr. readtoend (); // obtain the HTML to be output
Strmrdr. Dispose ();
Mem. Dispose ();
// Set the breakpoint here to view the value of pagecontent
// Session ["htmlcode"] = pagecontent;
Writer. Write (pagecontent); write to the output stream

}

 

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.