ASP.NET Response.Filter [英] ASP.NET Response.Filter

查看:99
本文介绍了ASP.NET Response.Filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建将HTML中的标签<h2>替换为<h3>的过滤器:

I need to create filter that replace tags <h2> in the HTML to <h3>:

我的过滤器

public class TagsFilter:Stream
{
    HttpContext qwe;

    public TagsFilter(HttpContext myContext)
    {
        qwe = myContext;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        string html = System.Text.Encoding.UTF8.GetString(buffer);
        html = html.Replace("<h2>", "<h3>");
        qwe.Response.Write(html.ToCharArray(), 0, html.ToCharArray().Length);
    }

我的模块

public class TagsChanger : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.Response.Filter = new TagsFilter(context.Context);
    }

我收到错误System.Web.HttpException:在这种情况下,答案不可用.

I get error System.Web.HttpException:In this context, the answer is not available.

推荐答案

查看Rick Strahl关于.

Look at Rick Strahl's post about "Capturing and Transforming ASP.NET Output with Response.Filter".

响应.过滤器内容已分块.因此,要有效地实现Response.Filter,只需要实现自定义流并处理Write()方法即可在编写响应时捕获响应输出.乍一看,这似乎很简单–您可以在Write中捕获输出,对其进行转换,然后一次性写入转换后的内容.这确实适用于少量内容.但是您看到的问题是,输出是用小的缓冲区块(看起来少于16k)写的,而不是仅将单个Write()语句写入流中,这对于ASP.NET将数据流式传输到其中非常有意义. IIS以较小的块为单位,以最大程度地减少途中的内存使用.

Response.Filter content is chunked. So to implement a Response.Filter effectively requires only that you implement a custom stream and handle the Write() method to capture Response output as it’s written. At first blush this seems very simple – you capture the output in Write, transform it and write out the transformed content in one pass. And that indeed works for small amounts of content. But you see, the problem is that output is written in small buffer chunks (a little less than 16k it appears) rather than just a single Write() statement into the stream, which makes perfect sense for ASP.NET to stream data back to IIS in smaller chunks to minimize memory usage en route.

不幸的是,这也使实施任何过滤例程变得更加困难,因为您无法直接访问所有响应内容,这是有问题的,特别是如果那些过滤例程需要您查看整个响应以进行转换或捕获绅士在我的会话中要求的解决方案所需的输出.

Unfortunately this also makes it a more difficult to implement any filtering routines since you don’t directly get access to all of the response content which is problematic especially if those filtering routines require you to look at the ENTIRE response in order to transform or capture the output as is needed for the solution the gentleman in my session asked for.

因此,为了解决此问题,需要一种略有不同的方法,该方法基本上捕获传递到缓存流中的所有Write()缓冲区,然后仅在流完成并准备好进行刷新时才使该流可用.

So in order to address this a slightly different approach is required that basically captures all the Write() buffers passed into a cached stream and then making the stream available only when it’s complete and ready to be flushed.

在考虑实现时,我还开始考虑使用Response.Filter实现时的几个实例.每次我必须创建一个新的Stream子类并创建我的自定义功能时,最后每个实现都做同样的事情-捕获输出并对其进行转换.我认为应该通过创建可重用的Stream类来实现此目的的更简单方法,该类可以处理Response.Filter实现中常见的流转换.

As I was thinking about the implementation I also started thinking about the few instances when I’ve used Response.Filter implementations. Each time I had to create a new Stream subclass and create my custom functionality but in the end each implementation did the same thing – capturing output and transforming it. I thought there should be an easier way to do this by creating a re-usable Stream class that can handle stream transformations that are common to Response.Filter implementations.

Rick Strahl编写了自己的流过滤器实现,允许以正确的方式替换文本.

Rick Strahl wrote own implementation of stream filter that permits text replacing in right way.

这篇关于ASP.NET Response.Filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆