【问题标题】:HttpResponse filter returns nothingHttpResponse 过滤器不返回任何内容
【发布时间】:2010-11-20 12:57:26
【问题描述】:

我编写了一个 HttpModule,用于拦截 WebResource.axd 处理程序的调用,以便我可以对 javascript 执行一些后期处理。

模块包装 Response.Filter 流以执行其处理并将其更改写入底层流。

我遇到的问题是脚本没有返回到浏览器。

因此,作为一个非常简单的示例,该模块仅用作传递,该模块如下所示:

 public class ResourceModule : IHttpModule
{
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
    }

    void context_PostRequestHandlerExecute(object sender, EventArgs e)
    {
        HttpApplication context = sender as HttpApplication;

        if (context.Request.Url.ToString().Contains("WebResource.axd"))
        {
            context.Response.Filter = new ResourceFilter(context.Response.Filter);
        }
    }
}

仅输出它接收到的内容的 ResourceFilter 如下所示:

 public class ResourceFilter : MemoryStream
{
    private Stream inner;

    public ResourceFilter(Stream inner)
    {
        this.inner = inner;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        inner.Write(buffer, offset, count);
    }
}

我可以附加并查看正在调用的模块和过滤器,但是当我浏览到 WebResource.axd url 时,我什么也得不到。

我已经使用这种模式来实现在 aspx 页面上执行处理的模块,它们工作得很好。似乎与 WebResource.axd 的交互有一些东西阻止了这个工作。

【问题讨论】:

    标签: c# asp.net filter httpmodule


    【解决方案1】:

    我做了一个小项目并准确地重现了您的问题。我正在运行 fiddler 以仔细查看响应,包括标题,发现它仅在发生这种情况的 *.axd 文件的过滤器上。

    经过一番搜索,我找到了 Daniel Richardson 的 this 文章,他遇到了同样的问题。

    原来System.Web.Handlers.AssemblyResourceLoader(axds 经过)设置了一个标志以忽略进一步的写入。

    Daniel 给出了一个使用反射取消设置此标志并允许您的过滤器处理 axd 的结果的示例。我试过了,效果很好。最好记住这对性能的任何影响,正如 Daniel 所说,ASP.NET 实现可能会发生变化。

    【讨论】:

    • 如果有人对将标志设置为false的代码感兴趣:FieldInfo field = _webStream.GetType().BaseType.GetField("_writer", BindingFlags.NonPublic | BindingFlags.Instance);var writer = field.GetValue(_webStream);FieldInfo ignoringWrites = writer.GetType().GetField("_ignoringFurtherWrites", BindingFlags.NonPublic | BindingFlags.Instance);ignoringWrites.SetValue(writer, false);
    【解决方案2】:

    根据我的经验,过滤器最迟需要在 PreRequestHandlerExecute 事件中“挂钩”,以使其在早于版本 7 的 IIS 版本中工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-24
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多