One previous post has written about a WebForm version of the block HTML output stream, which has recently been used in the same way as MVC.
Such as
Check for a long time and do not know what the reason.
Well, I finally chose to give up.
Recall the previous custom Response.filter, the inside write method can get the information of the page flow.
This time I borrowed the HttpModule implementation to intercept the HTML content output stream, see the code below
First, HtmlHttpModule.cs defines a new class inheritance HttpModule
Using system;using system.collections.generic;using system.io;using system.linq;using System.Reflection;using System.text;using system.threading.tasks;using system.web;namespace monitor{public class Htmlhttpmodule:ihttpmodule {Private HttpApplication _contextapplication; Private TextWriter Tw_old; Old Stream 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) {#region try {_content = new StringBuilder (); _contextapplication.response.filter = new Defaultfilter (_contextapplication.response.filter, o = _content. Append (o)); Use a custom filter filter} catch (Exception ex) {//write to log here} #endregion} void _contextapplication_endrequest (object sender, EventArgs e) {#region try {
#region
This is the code that writes the HTML. Now the HTML text is stored in the _content, and the removal process can be
After processing, release the following write
#endregion
_contextapplication.response.write (_content. ToString ()); } catch (Exception ex) { //write here to log } #endregion } public void Dispose () { _ Contextapplication = null; if (_contextapplication! = null) { _contextapplication.dispose () ; }}}}
Second, DefaultFilter.cs in module we give Response.filter a custom filter
Using system;using system.collections.generic;using system.linq;using system.text;using System.IO;using System.text.regularexpressions;using system.web;namespace monitor{public class Defaultfilter:stream {Stre Am Responsestream; Long position; Action<string> Action; Public defaultfilter (Stream inputstream,action<string> Act) {Action = act; Responsestream = InputStream; } public override bool CanRead {get {return responsestream.canread; }} public override bool CanSeek {get {return respon Sestream.canseek; }} public override bool CanWrite {get {return responsestream. CanWrite; }} public override long Length {get {return responsestream.le Ngth; }} public override long Position {get {return Position; } set {position = value; }} public override void Flush () {Responsestream.flush (); } public override int Read (byte[] buffer, int offset, int count) {return Responsestream.read (buf Fer, offset, count); } public override long Seek (long offset, SeekOrigin origin) {return Responsestream.seek (offset, Origin); public override void SetLength (Long value) {responsestream.setlength (value); } public override void Write (byte[] buffer, int offset, int count) {action (HTTPCONTEXT.CURRENT.R Esponse. contentencoding.getstring (buffer, offset, count)); } }}
Iii. Web. config The creation is all created, now let it work in MVC.
<system.webServer> <modules> <add name= "MONOTPR" type= "Monitor.htmlhttpmodule,monitor"/ > </modules> </system.webServer>
Block ASP. NET MVC output stream for processing, block HTML text (ASP. NET MVC version)