HttpResponse filter returns nothing

I wrote the HttpModule, which I use to intercept, calls the WebResource.axd handler, so I can do some post processing on javascript.

The module wraps the Response.Filter stream to execute its processing and writes it to the base stream.

The problem is that the script is not returning to the browser.

So, as a really simple example that just acts like a pass, 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 ResourceFilter, which simply 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 how the module and filter are called, but when I look at the URL of WebResource.axd, I get nothing.

I used this template to implement modules that do processing on aspx pages, and they work very well. There seems to be something about interacting with WebResource.axd that is preventing this from working.

+4
c # filter
source share
2 answers

I did a small project and recreated your problem for sure. I ran the violinist to get a good look at the answer, including the headers, and found that it was only in the filters in the * .axd files where this happened.

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

It turns out that System.Web.Handlers.AssemblyResourceLoader (which runs along the axis) sets a flag to ignore further entries.

Daniel gives an example of using reflection to clear this flag and allow your filter to work with the result of axd. I tried and it works well. It is better to keep in mind any performance impact, although Daniel says that ASP.NET implementations may change.

+5
source

Based on my experience, the filter must be β€œconnected” to the PreRequestHandlerExecute event in order for it to work in IIS version older than version 7.

+1
source

All Articles