zoukankan      html  css  js  c++  java
  • 拦截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 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>
    
  • 相关阅读:
    使用react hook做一个小型完整项目(包括二级路由,动态路由,redux,tab切换,列表跳详情,登录, 守卫)
    项目实战【vue,react,微信小程序】(1705E)
    Vue(1706E)
    加入购物车动画(css)
    React从入门到精通(1704B)
    React(1702H)文章管理-cms系统
    React(1702H)文件上传和分页查找
    React (1702H) 点击复制、滚动条、图形验证码、ridis、密码rsa加密、token、发邮件、文件上传、sql语句
    位图算法
    def跨域+jwt
  • 原文地址:https://www.cnblogs.com/sjns/p/4242056.html
Copyright © 2011-2022 走看看