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