Interception of ASP. NET output Stream processing

Source: Internet
Author: User
Tags httpcontext

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
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Web;
  5. Using System.Web.UI;
  6. Using System.Web.UI.WebControls;
  7. Using System.Text;
  8. Using System.IO;
  9. Using System.Reflection;
  10. Public partial class _default:system.web.ui.page
  11. {
  12. StringBuilder content = new StringBuilder ();
  13. TextWriter Tw_old, tw_new;
  14. FieldInfo Tw_field;
  15. protected void Page_Load (object sender, EventArgs e)
  16. {
  17. var context = HttpContext.Current;
  18. Tw_old = context. Response.Output; //response the original output
  19. Tw_new = New StringWriter (content); A stringwriter to get the page content
  20. var TYPE_RP = context. Response.gettype ();
  21. //Get the private field of an object by reflection
  22. Tw_field = Type_rp. GetField ("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |  System.Reflection.BindingFlags.Instance);
  23. Tw_field. SetValue (context. Response, tw_new);
  24. }
  25. protected override void Render (HtmlTextWriter writer)
  26. {
  27. base.  Render (writer);
  28. //Replace output of response back
  29. Tw_field. SetValue (HttpContext.Current.Response, tw_old);
  30. //Do your own processing
  31. Content.  Appendline ("<!--Lake----");
  32. HttpContext.Current.Response.Write (content. ToString ());
  33. }
  34. }
  35. Method two, using Httpmodul to achieve:
  36. Using System;
  37. Using System.Collections.Generic;
  38. Using System.Linq;
  39. Using System.Web;
  40. Using System.Web.UI;
  41. Using System.IO;
  42. Using System.Text;
  43. Using System.Reflection;
  44. <summary>
  45. Summary description of HttpModule
  46. </summary>
  47. Public class Httpmodule:ihttpmodule
  48. {
  49. private HttpApplication _contextapplication;
  50. private TextWriter tw_new, Tw_old;
  51. private StringBuilder _content;
  52. private FieldInfo Tw_field;
  53. public void Init (HttpApplication context)
  54. {
  55. _contextapplication = context;
  56. _contextapplication.prerequesthandlerexecute + = new EventHandler (_contextapplication_prerequesthandlerexecute  );
  57. }
  58. public void Dispose ()
  59. {
  60. _contextapplication = null;
  61. _contextapplication.dispose ();
  62. }
  63. public void _contextapplication_prerequesthandlerexecute (object sender, EventArgs e)
  64. {
  65. HttpContext context = _contextapplication.context;
  66. var _page = context.  Handler as System.Web.UI.Page;
  67. _page.  Unload + = new EventHandler (_page_unload);
  68. _content = new StringBuilder ();
  69. Tw_old = context. Response.Output; //response the original output
  70. Tw_new = new StringWriter (_content); A stringwriter to get the page content
  71. var TYPE_RP = context. Response.gettype ();
  72. Tw_field = Type_rp. GetField ("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |  System.Reflection.BindingFlags.Instance);
  73. Tw_field. SetValue (context. Response, tw_new);
  74. }
  75. void _page_unload (object sender, EventArgs e)
  76. {
  77. //Replace output of response back
  78. Tw_field. SetValue (HttpContext.Current.Response, tw_old);
  79. //Do your own processing
  80. _content.  Appendline ("<!--Lake----");
  81. HttpContext.Current.Response.Write (_content. ToString ());
  82. }
  83. }
  84. Method Three:
  85. Public class Httpmodule:ihttpmodule
  86. {
  87. private HttpApplication _contextapplication;
  88. private TextWriter tw_new, Tw_old;
  89. private StringBuilder _content;
  90. private FieldInfo Tw_field;
  91. public void Init (HttpApplication application)
  92. {
  93. _contextapplication = Application;
  94. _contextapplication.beginrequest + = new EventHandler (_contextapplication_beginrequest);
  95. _contextapplication.endrequest + =new EventHandler (_contextapplication_endrequest);
  96. }
  97. void _contextapplication_beginrequest (object sender, EventArgs e)
  98. {
  99. _content = new StringBuilder ();
  100. Tw_old = _contextapplication.response.output;
  101. Tw_new = new StringWriter (_content);
  102. var type_rp = _contextapplication.response.gettype ();
  103. Tw_field = Type_rp. GetField ("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic |  System.Reflection.BindingFlags.Instance);
  104. Tw_field. SetValue (_contextapplication.response, tw_new);
  105. }
  106. void _contextapplication_endrequest (object sender, EventArgs e)
  107. {
  108. Tw_field. SetValue (_contextapplication.response, tw_old);
  109. //Do your own processing
  110. _content.  Appendline ("<!--jhxz-->");
  111. _contextapplication.response.write (_content. ToString ());
  112. }
  113. public void Dispose ()
  114. {
  115. _contextapplication = null;
  116. _contextapplication.dispose ();
  117. }
  118. }

Finally, recommend a good article: yards of the European business trip a little episode

Interception of ASP. NET output Stream processing

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.