Method of intercepting and processing asp.net output stream, asp.net output stream
The examples in this article mainly implement some processing before HTML pages are generated and output to the client.
The implementation principle of the method is: redirect the Response output to the custom container, that is, in our StringBuilder object, all HTML outputs to the page are output to StringBuilder, after processing StringBuilder, redirect the Response output to the original page, and then use Response. the Write method outputs the content of StringBuilder to the page.
Reflection is used here because the OutPut attribute of the Response object is read-only. By decompiling the assembly of this class, it is found that OutPut is actually implemented by internal private member _ writer. Therefore, the value of this Member is rewritten through reflection to redirect the output stream.
The specific function code is as follows:
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using System. text; using System. IO; using System. reflection; public partial class _ Default: System. web. UI. page {StringBuilder content = new StringBuilder (); TextWriter tw_old, tw_new; FieldInfo tw_field; protected void Page_Load (object sender, EventArgs e) {v Ar context = HttpContext. current; tw_old = context. response. output; // Response Original OutPut tw_new = new StringWriter (content); // A StringWriter used to obtain the page content var type_rp = context. response. getType (); // obtain the private field tw_field = type_rp.GetField ("_ writer", System. reflection. bindingFlags. public | System. reflection. bindingFlags. nonPublic | System. reflection. bindingFlags. instance); tw_field.SetValue (context. resp Onse, tw_new);} protected override void Render (HtmlTextWriter writer) {base. render (writer); // Replace the OutPut tw_field.SetValue (HttpContext. current. response, tw_old); // do your own processing content. appendLine ("<! -- Jianghu kiddies --> "); HttpContext. Current. Response. Write (content. ToString ());}}
Method 2: Use HttpModul to implement:
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. IO; using System. text; using System. reflection; // <summary> // summary of HttpModule /// </summary> public class HttpModule: IHttpModule {private HttpApplication _ contextApplication; private TextWriter tw_new, tw_old; private StringBuilder _ content; private FieldInfo tw_field; publi C void Init (HttpApplication context) {_ contextApplication = context; _ contextApplication. preRequestHandlerExecute + = new EventHandler (_ contextApplication_PreRequestHandlerExecute);} public void Dispose () {_ contextApplication = null; _ contextApplication. dispose ();} public void _ contextApplication_PreRequestHandlerExecute (object sender, EventArgs e) {HttpContext context = _ contextApplication. co Ntext; var _ page = context. handler as System. web. UI. page; _ page. unload + = new EventHandler (_ page_Unload); _ content = new StringBuilder (); tw_old = context. response. output; // Response Original OutPut tw_new = new StringWriter (_ content); // A StringWriter, used to obtain the page content var type_rp = context. response. getType (); tw_field = type_rp.GetField ("_ writer", System. reflection. bindingFlags. public | System. reflection. bindingF Lags. nonPublic | System. reflection. bindingFlags. instance); tw_field.SetValue (context. response, tw_new);} void _ page_Unload (object sender, EventArgs e) {// Replace the OutPut tw_field.SetValue (HttpContext. current. response, tw_old); // do your own processing _ content. appendLine ("<! -- Jianghu kiddies --> "); HttpContext. Current. Response. Write (_ content. ToString ());}}
Method 3:
Public class HttpModule: IHttpModule {private HttpApplication _ contextApplication; private TextWriter tw_new, tw_old; private StringBuilder _ content; private FieldInfo tw_field; public void Init (HttpApplication application) {_ contextApplication = application; _ contextApplication. beginRequest + = new EventHandler (_ contextApplication_BeginRequest); _ contextApplication. endRequest + = new EventHandler (_ ContextApplication_EndRequest);} void _ contextApplication_BeginRequest (object sender, EventArgs e) {_ content = new StringBuilder (); tw_old = _ contextApplication. response. output; tw_new = new StringWriter (_ content); var type_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 (object sender, EventArgs e) {tw_field.SetValue (_ contextApplication. response, tw_old); // do your own processing _ content. appendLine ("<! -- Jhxz --> "); _ contextApplication. response. write (_ content. toString ();} public void Dispose () {_ contextApplication = null; _ contextApplication. dispose ();}}
I believe this article has some reference value for your asp.net program design.
Aspnet respone output stream problems
System. IO. StringWriter sw = new System. IO. StringWriter ();
HtmlTextWriter hw = new HtmlTextWriter (sw );
Datagrid. RenderControl (hw );
Response. Clear ();
Response. Write (sw );
Response. End ();
Asp net asynchronous calling of output streams
First, you write
OBao. open ("Post", "ao. aspx", false );
The last parameter "false" indicates synchronous operation. "true" indicates asynchronous operation.
If (! IsPostBack)
{
Response. Write ("testajax ");
Response. End ();
}
In this way, only testajax can be obtained.