The title of this article refers to the processing of a page that has generated HTML before it is output to the client.
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.
[C-sharp]View Plaincopy
- 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 the original output
- Tw_new = New StringWriter (content); A stringwriter to get the page content
- var TYPE_RP = context. Response.gettype ();
- //Get 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 output of response back
- Tw_field. SetValue (HttpContext.Current.Response, tw_old);
- //Do your own processing
- Content. Appendline ("<!--Lake----");
- HttpContext.Current.Response.Write (content. ToString ());
- }
- }
- Method two, using Httpmodul to achieve:
- 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 description of HttpModule
- </summary>
- Public class Httpmodule:ihttpmodule
- {
- private HttpApplication _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 the original output
- Tw_new = new StringWriter (_content); A stringwriter to get the 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 output of response back
- Tw_field. SetValue (HttpContext.Current.Response, tw_old);
- //Do your own processing
- _content. Appendline ("<!--Lake----");
- 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 ();
- }
- }
Finally, recommend a good article: yards of the European business trip a little episode
Interception of ASP. NET output Stream processing