以前的一个贴子写过一个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>

拦截asp.net mvc输出流做处理, 拦截HTML文本(asp.net MVC版)的更多相关文章

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

    对已经生成了HTML的页面做一些输出到客户端之前的处理 方法的原理是:把Response的输出重定向到自定义的容器内,也就是我们的StringBuilder对象里,在HTML所有的向页面输出都变 成了 ...

  2. 程序猿修仙之路--数据结构之你是否真的懂数组? c#socket TCP同步网络通信 用lambda表达式树替代反射 ASP.NET MVC如何做一个简单的非法登录拦截

    程序猿修仙之路--数据结构之你是否真的懂数组?   数据结构 但凡IT江湖侠士,算法与数据结构为必修之课.早有前辈已经明确指出:程序=算法+数据结构  .要想在之后的江湖历练中通关,数据结构必不可少. ...

  3. 在ASP.NET Core MVC中子类Controller拦截器要先于父类Controller拦截器执行

    我们知道在ASP.NET Core MVC中Controller上的Filter拦截器是有执行顺序的,那么如果我们在有继承关系的两个Controller类上,声明同一种类型的Filter拦截器,那么是 ...

  4. sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)

    sql server 关于表中只增标识问题   由于我们系统时间用的过长,数据量大,设计是采用自增ID 我们插入数据的时候把ID也写进去,我们可以采用 关闭和开启自增标识 没有关闭的时候 ,提示一下错 ...

  5. 0002 - Spring MVC 拦截器源码简析:拦截器加载与执行

    1.概述 Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理.例如通过拦截器可以进行权限验证.记录请求信息的日 ...

  6. ASP.NET Core 3.0 gRPC 拦截器

    目录 ASP.NET Core 3.0 使用gRPC ASP.NET Core 3.0 gRPC 双向流 ASP.NET Core 3.0 gRPC 拦截器 一. 前言 前面两篇文章给大家介绍了使用g ...

  7. Spring MVC全局异常处理与拦截器校检

    在使用Spring MVC进行开发时,总是要对系统异常和用户的异常行为进行处理,以提供给用户友好的提示,也可以提高系统的安全性. 拦截系统响应错误 首先是拦截系统响应错误,这个可以在web.xml中配 ...

  8. Spring3中的mvc:interceptors标签配置拦截器

    mvc:interceptors 这个标签用于注册一个自定义拦截器或者是WebRequestInterceptors. 可以通过定义URL来进行路径请求拦截,可以做到较为细粒度的拦截控制. 例如在配置 ...

  9. Spring MVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

随机推荐

  1. IIS SSL取消证书合法性验证

    cscript adsutil.vbs set w3svc/certcheckmode 1win 2003 IIS 6 以下执行以上代码,去除证书的合法性验证 cscript adsutil.vbs ...

  2. Code Sign error: No code signing identities found: No valid signing identities

    Code Sign error: No code signing identities found: No valid signing identities 解决办法:如果证书可获取,最简办法就是把所 ...

  3. 关于function

    场景:让用户输入一个数字,程序由1+2....一直累加到用户输入的数字为止 #!/bin/bashPATH=$PATH:~/scriptexport PATH #chech whether the i ...

  4. libimobiledevice命令

    Mac 安装 1. 安装HomeBrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/m ...

  5. javascript中的面向对象—— 学习1

    面向对象:Object Oriented(OO) 一切事物皆对象,通过面向对象的方式,将显示世界的事物抽象成对象,将显示世界中的关系抽象成类.继承,帮助人们实现对显示世界的抽象与数字建模:--百科 一 ...

  6. Debian系Linux的dpkg命令

    dpkg "是"Debian Packager "的简写.为 "Debian" 专门开发的套件管理系统,方便软件的安装.更新及移除.所有源自" ...

  7. SQL Server 查看数据库是否存在阻塞

    CREATE procedure [dbo].[sp_who_lock] as begin declare @spid int,@bl int, @intTransactionCountOnEntry ...

  8. 模仿快递路线图的html, css 样式

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  9. C#委托与事件讲解(一)

    首先,我们还是先说说委托吧,从字面上理解,只要是中国人应该都知道这个意思,除非委托2个中文字不认识,举个例子,小明委托小张去买车票.     但是在我们的程序世界里,也是这么的简单吗?当然,什么是OO ...

  10. python之~【空格】可不能随便加唷~

    上个礼拜学习从.proto文件转xxpb2.py文件的时候,明明成功了的. 结果周末的时候在家里,以及今天周一来到公司电脑,都遇到同样的一个问题. 我就纳闷了.这个路径确实存在呀. 而且我找遍了搜索引 ...