Ways to intercept and process asp.net output streams-practical tips

Source: Internet
Author: User

This article mainly implements the processing of the pages that have already generated HTML to do some output to the client.

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

The reason for this is reflection, because the output property of the response object is read-only, and by recompiling the class's assembly it is found that outputting is actually an internal private member _writer to implement it. Therefore, the value of the member is overwritten by reflection to achieve the redirection of 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) {var context = HttpContext.Current; Tw_old = context. Response.output;//response original Output tw_new = new StringWriter (content);//a StringWriter to get page content var type_rp = C Ontext. 
    Response.gettype (); Gets the private field of an object by reflection 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); } protected override void Render (HtmlTextWriter writer) {base. 
    Render (writer); 
Replace back to response output    Tw_field. 
    SetValue (HttpContext.Current.Response, tw_old); Do your own processing content. 
    Appendline ("<!---->"); HttpContext.Current.Response.Write (content. 
  ToString ()); 
  } 
}

Method Two, implemented with Httpmodul:   

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>///httpmodule's summary description///</summary> public class Httpmodule:ihttpmodule {private HTTPAP 
  Plication _contextapplication; 
  Private TextWriter tw_new, Tw_old; 
  Private StringBuilder _content; 
  Private FieldInfo Tw_field; 
    public 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.context; 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 get page content var type_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 (object sender, EventArgs e) {//replace back to response output Tw_field. 
    SetValue (HttpContext.Current.Response, tw_old); Do your own processing _content. 
    Appendline ("<!---->"); HttpContext.Current.Response.Write (_content. 
  ToString ()); 
  } 
}

Method Three:

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 ();

 } 
}

It is believed that this article has certain reference value to the ASP.net program design of our people.

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.