HttpResponse筛选器不会返回任何内容 [英] HttpResponse filter returns nothing

查看:163
本文介绍了HttpResponse筛选器不会返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



模块包装Response.Filter流来执行它的处理,并将其更改写入基础流。

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

所以作为一个非常简单的例子,它只是作为一个传递,模块看起来像这样:

<$ p $公共类ResourceModule:IHttpModule
{
public void Dispose()
{
}

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


void context_PostRequestHandlerExecute(object sender,EventArgs e)
{
HttpApplication context = sender as HttpApplication;
$ b $ if(context.Request.Url.ToString()。Contains(WebResource.axd))
{
context.Response.Filter = new ResourceFilter(context.Response 。过滤);



code
$ b

和刚刚输出的ResourceFilter它收到的是这样的:

$ p code $ public $ 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);




$ b $ p
$ b

我可以附加并查看被调用的模块和过滤器,但是当我浏览到WebResource.axd网址时,我什么也收不回来。



我使用这个模式来实现在aspx页面上执行处理的模块,它们工作得很好。看来有一些与WebResource.axd的交互,以防止这种工作。

解决方案

我做了一个小项目,并重新创建你的问题确切。我正在运行小提琴手,看看反应,包括标题,发现它只是在* .axd文件的过滤器发生这种情况。



经过一些搜索我找到 Daniel Richardson的文章有同样的问题。



结果发现 System.Web.Handlers.AssemblyResourceLoader (它通过)设置一个标志来忽略进一步写道。



丹尼尔给出了一个使用反射来取消设置这个标志的例子,并允许你的过滤器处理axd的结果。我试了一下,效果很好。最好记住这一点对性能的影响,正如丹尼尔所说,ASP.NET的实现可能会改变。


I have written a HttpModule that I am using to intercept calls the the WebResource.axd handler so I can perform some post processing on the javascript.

The module wraps the Response.Filter stream to perform its processing and writes it's changes to the underlying stream.

The problem I have is that the script does not get returned to the browser.

So as a really simple example that just acts as a pass through, the module looks like this:

 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);
        }
    }
}

and the ResourceFilter that just outputs what it receives looks like this:

 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);
    }
}

I can attach and see the module and filter being invoked, but when I browse to WebResource.axd url I get nothing back.

I have used this pattern to implement modules that perform processing on aspx pages and they work just fine. It appears there is something about the interaction with the WebResource.axd that prevents this working.

解决方案

I made a small project and recreated your problem exactly. I was running fiddler to have a good look at the response, including headers and found it was only on filters on *.axd files where this happened.

After some searching I found this article by Daniel Richardson who had the same issue.

Turns out that the System.Web.Handlers.AssemblyResourceLoader (which axds go through) sets a flag to ignore further writes.

Daniel gives an example of using reflection to unset this flag and allow your filter to work on the result of the axd. I tried it out and it works well. Best keep in mind any performance impact of this though, and as Daniel says, the ASP.NET implementations could change.

这篇关于HttpResponse筛选器不会返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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