国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

攔截asp.net輸出流做處理

2019-11-17 01:43:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

攔截asp.net輸出流做處理

本文標(biāo)題是指對(duì)已經(jīng)生成了HTML的頁(yè)面做一些輸出到客戶端之前的處理。

方法的原理是:把Response的輸出重定向到自定義的容器內(nèi),也就是我們的StringBuilder對(duì)象里,在HTML所有的向頁(yè)面輸出都變成了向StringBuilder輸出,然后我們對(duì)StringBuilder處理完成之后,再把Response的輸出重定向到原來(lái)的頁(yè)面上,然后再通過(guò)Response.Write方法把StringBuilder的內(nèi)容輸出到頁(yè)面上

這里之所以用反射,是因?yàn)镽esponse對(duì)象的OutPut屬性是只讀的,通過(guò)反編譯該類的程序集發(fā)現(xiàn),OutPut實(shí)際上是內(nèi)部私有成員 _writer來(lái)實(shí)現(xiàn)輸出的。因此通過(guò)反射來(lái)改寫該成員的值以實(shí)現(xiàn)輸出流的重定向。

[c-sharp]view plaincopy
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Web;
  5. usingSystem.Web.UI;
  6. usingSystem.Web.UI.WebControls;
  7. usingSystem.Text;
  8. usingSystem.IO;
  9. usingSystem.Reflection;
  10. publicpartialclass_Default:System.Web.UI.Page
  11. {
  12. StringBuildercontent=newStringBuilder();
  13. TextWritertw_old,tw_new;
  14. FieldInfotw_field;
  15. PRotectedvoidPage_Load(objectsender,EventArgse)
  16. {
  17. varcontext=HttpContext.Current;
  18. tw_old=context.Response.Output;//Response原來(lái)的OutPut
  19. tw_new=newStringWriter(content);//一個(gè)StringWriter,用來(lái)獲取頁(yè)面內(nèi)容
  20. vartype_rp=context.Response.GetType();
  21. //通過(guò)反射獲取對(duì)象的私有字段
  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. protectedoverridevoidRender(HtmlTextWriterwriter)
  26. {
  27. base.Render(writer);
  28. //替換回Response的OutPut
  29. tw_field.SetValue(HttpContext.Current.Response,tw_old);
  30. //做自己的處理
  31. content.AppendLine("<!--江湖小子-->");
  32. HttpContext.Current.Response.Write(content.ToString());
  33. }
  34. }
  35. 方法二,用HttpModul來(lái)實(shí)現(xiàn):
  36. usingSystem;
  37. usingSystem.Collections.Generic;
  38. usingSystem.Linq;
  39. usingSystem.Web;
  40. usingSystem.Web.UI;
  41. usingSystem.IO;
  42. usingSystem.Text;
  43. usingSystem.Reflection;
  44. ///<summary>
  45. ///HttpModule的摘要說(shuō)明
  46. ///</summary>
  47. publicclassHttpModule:IHttpModule
  48. {
  49. privateHttpapplication_contextApplication;
  50. privateTextWritertw_new,tw_old;
  51. privateStringBuilder_content;
  52. privateFieldInfotw_field;
  53. publicvoidInit(HttpApplicationcontext)
  54. {
  55. _contextApplication=context;
  56. _contextApplication.PreRequestHandlerExecute+=newEventHandler(_contextApplication_PreRequestHandlerExecute);
  57. }
  58. publicvoidDispose()
  59. {
  60. _contextApplication=null;
  61. _contextApplication.Dispose();
  62. }
  63. publicvoid_contextApplication_PreRequestHandlerExecute(objectsender,EventArgse)
  64. {
  65. HttpContextcontext=_contextApplication.Context;
  66. var_page=context.HandlerasSystem.Web.UI.Page;
  67. _page.Unload+=newEventHandler(_page_Unload);
  68. _content=newStringBuilder();
  69. tw_old=context.Response.Output;//Response原來(lái)的OutPut
  70. tw_new=newStringWriter(_content);//一個(gè)StringWriter,用來(lái)獲取頁(yè)面內(nèi)容
  71. vartype_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(objectsender,EventArgse)
  76. {
  77. //替換回Response的OutPut
  78. tw_field.SetValue(HttpContext.Current.Response,tw_old);
  79. //做自己的處理
  80. _content.AppendLine("<!--江湖小子-->");
  81. HttpContext.Current.Response.Write(_content.ToString());
  82. }
  83. }
  84. 方法三:
  85. publicclassHttpModule:IHttpModule
  86. {
  87. privateHttpApplication_contextApplication;
  88. privateTextWritertw_new,tw_old;
  89. privateStringBuilder_content;
  90. privateFieldInfotw_field;
  91. publicvoidInit(HttpApplicationapplication)
  92. {
  93. _contextApplication=application;
  94. _contextApplication.BeginRequest+=newEventHandler(_contextApplication_BeginRequest);
  95. _contextApplication.EndRequest+=newEventHandler(_contextApplication_EndRequest);
  96. }
  97. void_contextApplication_BeginRequest(objectsender,EventArgse)
  98. {
  99. _content=newStringBuilder();
  100. tw_old=_contextApplication.Response.Output;
  101. tw_new=newStringWriter(_content);
  102. vartype_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(objectsender,EventArgse)
  107. {
  108. tw_field.SetValue(_contextApplication.Response,tw_old);
  109. //做自己的處理
  110. _content.AppendLine("<!--jhxz-->");
  111. _contextApplication.Response.Write(_content.ToString());
  112. }
  113. publicvoidDispose()
  114. {
  115. _contextApplication=null;
  116. _contextApplication.Dispose();
  117. }
  118. }

最后還是推薦一篇好文:碼農(nóng)歐洲出差的一點(diǎn)小插曲


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜章县| 马山县| 哈密市| 巫山县| 伊宁市| 阳谷县| 涟源市| 凯里市| 江山市| 平定县| 绥德县| 临颍县| 哈巴河县| 万山特区| 合江县| 海阳市| 台安县| 仁布县| 屏边| 尚志市| 延边| 广丰县| 顺平县| 永平县| 和田市| 晋宁县| 平乐县| 日土县| 庄浪县| 桓仁| 西吉县| 安康市| 邵武市| 枣庄市| 波密县| 乌鲁木齐县| 来凤县| 米易县| 夏津县| 凤山县| 陵水|