Block ASP. Block the output stream for processing, blocking HTML text

Source: Internet
Author: User
Tags httpcontext

The processing of some output to the client before the HTML-generated page

The principle of the method is: Redirect the output of response to the custom container, that is, our StringBuilder object, in the HTML all the page output to the StringBuilder output, Then we finish the StringBuilder processing, then redirect the output of response to the original page, and then output the StringBuilder content to the page by Response.Write method.

Reflection is used here because the output property of the response object is read-only, and by deserializing the assembly of the class, it is actually the internal private member _writer to implement the outputs. Therefore, the value of the member is overwritten by reflection to implement the redirection of the output stream. ( third method tested, feasible )

    usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingsystem.web; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingSystem.Text; usingSystem.IO; usingSystem.Reflection;  Public Partial class_default:system.web.ui.page {StringBuilder content=NewStringBuilder ();          TextWriter Tw_old, tw_new;                FieldInfo Tw_field; protected voidPage_Load (Objectsender, EventArgs e) {              varContext =HttpContext.Current; Tw_old= Context. Response.Output;//response the original outputTw_new =NewStringWriter (content);//a StringWriter to get the page content            varTYPE_RP =context.              Response.gettype (); //get the private fields of an object by reflectionTw_field = Type_rp. GetField ("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |System.Reflection.BindingFlags.Instance); Tw_field. SetValue (context.          Response, tw_new); }                protected Override voidRender (HtmlTextWriter writer) {Base.              Render (writer); //replace output back to responseTw_field.              SetValue (HttpContext.Current.Response, tw_old); //do your own processing.Content. Appendline ("<!--Lake ---"); HttpContext.Current.Response.Write (content.          ToString ()); }} method two, implemented with Httpmodul:usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingsystem.web; usingSystem.Web.UI; usingSystem.IO; usingSystem.Text; usingSystem.Reflection; /// <summary>      ///Summary description of HttpModule/// </summary>       Public classHttpmodule:ihttpmodule {PrivateHttpApplication _contextapplication; PrivateTextWriter tw_new, Tw_old; PrivateStringBuilder _content; PrivateFieldInfo Tw_field;  Public voidInit (HttpApplication context) {_contextapplication=context; _contextapplication.prerequesthandlerexecute+=NewEventHandler (_contextapplication_prerequesthandlerexecute); }                 Public voidDispose () {_contextapplication=NULL;          _contextapplication.dispose (); }                 Public void_contextapplication_prerequesthandlerexecute (Objectsender, EventArgs e) {HttpContext context=_contextapplication.context; var_page = context. Handler asSystem.Web.UI.Page; _page. Unload+=NewEventHandler (_page_unload); _content=NewStringBuilder (); Tw_old= Context. Response.Output;//response the original outputTw_new =NewStringWriter (_content);//a StringWriter to get the page content            varTYPE_RP =context.              Response.gettype (); Tw_field= Type_rp. GetField ("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |System.Reflection.BindingFlags.Instance); Tw_field. SetValue (context.          Response, tw_new); }                      void_page_unload (Objectsender, EventArgs e) {              //replace output back to responseTw_field.              SetValue (HttpContext.Current.Response, tw_old); //do your own processing._content. Appendline ("<!--Lake ---"); HttpContext.Current.Response.Write (_content.          ToString ()); }} Method Three: Public classHttpmodule:ihttpmodule {PrivateHttpApplication _contextapplication; PrivateTextWriter tw_new, Tw_old; PrivateStringBuilder _content; PrivateFieldInfo Tw_field;  Public voidInit (HttpApplication application) {_contextapplication=Application; _contextapplication.beginrequest+=NewEventHandler (_contextapplication_beginrequest); _contextapplication.endrequest+=NewEventHandler (_contextapplication_endrequest); }                void_contextapplication_beginrequest (Objectsender, EventArgs e) {_content=NewStringBuilder (); Tw_old=_contextapplication.response.output; Tw_new=NewStringWriter (_content); varTYPE_RP =_contextapplication.response.gettype (); Tw_field= Type_rp. GetField ("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |System.Reflection.BindingFlags.Instance); Tw_field.          SetValue (_contextapplication.response, tw_new); }                void_contextapplication_endrequest (Objectsender, EventArgs e) {Tw_field.              SetValue (_contextapplication.response, tw_old); //do your own processing._content. Appendline ("<!--jhxz-->"); _contextapplication.response.write (_content.          ToString ()); }                 Public voidDispose () {_contextapplication=NULL;          _contextapplication.dispose (); }      }  

Block ASP. Block the output stream for processing, blocking HTML text

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.