拦截asp.net mvc输出流做处理, 拦截HTML文本(asp.net MVC版)

时间:2023-03-08 21:28:00

  以前的一个贴子写过一个webForm的拦截HTML输出流的版本,最近用到mvc时用同样的方式发生一些问题。

  如下图

拦截asp.net mvc输出流做处理, 拦截HTML文本(asp.net MVC版)

查了好久也不知道啥原因。

好吧, 我最后选择放弃。

  想起以前自定义Response.Filter 时,里面Write方法可以获取页面流的信息。

  这次我借用HttpModule实现拦截HTML内容输出流,下面看代码

一、HtmlHttpModule.cs    定义一个新类继承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 StringBuilder _content; 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));
}
catch (Exception ex)
{
//这里写入日志
}
#endregion
} void _contextApplication_EndRequest(object sender, EventArgs e)
{
#region
try
{
//只处理页面,排除掉css、js、txt文件的请求
if (_contextApplication.Request.Headers["Accept"].StartsWith("text/html"))
{
_content.Append("<!--这是新加的-->");
}
_contextApplication.Response.Write(_content.ToString());
}
catch (Exception ex)
{
//这里写入日志
}
#endregion
} public void Dispose()
{
_contextApplication = null;
if (_contextApplication != null)
{
_contextApplication.Dispose();
}
}
  }
}

二、DefaultFilter.cs     在module中我们给Response.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
{
Stream 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 responseStream.CanSeek;
}
} public override bool CanWrite
{
get
{
return responseStream.CanWrite;
}
} public override long Length
{
get
{
return responseStream.Length;
}
} 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(buffer, 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.Response.ContentEncoding.GetString(buffer, offset, count));
}
}
}

三、web.config   该创建都创建好了,现在就让它在mvc中起作用吧

<system.webServer>
<modules>
<add name="Monotpr" type="Monitor.HtmlHttpModule,Monitor"/>
</modules>
</system.webServer>