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

  以前的一个贴子写过一个webForm的拦截HTML输出流的版本,最近用到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 TextWriter tw_old;    // 旧流
        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));   //用自定义的Filter筛选器
            }
            catch (Exception ex)
            {
                //这里写入日志
            }
            #endregion
        }

        void _contextApplication_EndRequest(object sender, EventArgs e)
        {
            #region 
            try
            {
          #region
          //这里就是写处理HTML的代码了。现在HTML的文本保存在_content中,取出处理就可以
          //处理完放出下面的Write中
          #endregion          
_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>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。