Intercept asp.net output stream for processing and intercept asp.net output stream

Source: Internet
Author: User
Tags asp net

Intercept asp.net output stream for processing and intercept asp.net output stream

The title of this article refers to the processing of HTML pages that have been generated before being output to the client.

The principle of the method is: redirect the Response output to the custom container, that is, in our StringBuilder object, all HTML output to the page is output to StringBuilder, after processing StringBuilder, redirect the Response output to the original page, and then use Response. write method to output 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.

 

 

[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 Original OutPut
  19. Tw_new = new StringWriter (content); // A StringWriter, used to obtain page content
  20. Var type_rp = context. Response. GetType ();
  21. // Obtain the private field of the object through 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 the OutPut of Response
  29. Tw_field.SetValue (HttpContext. Current. Response, tw_old );
  30. // Perform your own Processing
  31. Content. AppendLine ("<! -- Jianghu kiddies --> ");
  32. HttpContext. Current. Response. Write (content. ToString ());
  33. }
  34. }
  35. Method 2: Use HttpModul to implement:
  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 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 Original OutPut
  70. Tw_new = new StringWriter (_ content); // A StringWriter, used to obtain 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 the OutPut of Response
  78. Tw_field.SetValue (HttpContext. Current. Response, tw_old );
  79. // Perform your own Processing
  80. _ Content. AppendLine ("<! -- Jianghu kiddies --> ");
  81. HttpContext. Current. Response. Write (_ content. ToString ());
  82. }
  83. }
  84. Method 3:
  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. // Perform 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, I would like to recommend a good article: an episode on a business trip in Europe


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.

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.