HTTP Compression with ASP.NET 4.0 and IIS7

3.5k views Asked by At

I've been trying to implement HTTP compression on a few of my sites for some time now.

I've tried implementing HTTP compression using variations on the following two techniques:

From Application_BeginRequest in Global.asax

From a custom HttpModule

In both cases, it initially seems to work. But, as I navagate around the site, I quickly lose my CSS styling. And the brower reports a JS file contains invalid characters. And, at some point, the entire site turns entirely to jibberish.

I sure wish I knew what was happening here. Does anyone have any suggestion? Is it because these techniques only work on older versions of ASP.NET/IIS?

EDIT: I do not have access to IIS, and should not require it. With the latest iterations of ASP.NET and IIS, more and more control is being given to web.config without touching IIS. This should be fully supported without delving into IIS and, in fact, the two links above claim they are able to do just this. I just can't get it to work for me.

EDIT: On further inspection, using the code in the second link above (the HTTP module), I can see the module handler is being called for all file types on my site. This is clearly part of the problem. (Note that, in some cases, the entire page turns to garbage, which suggests other problems.) Can anyone suggest what determines which files are sent to the HTTP handler, or why the code at that link might work for someone else but not me?

2

There are 2 answers

0
Jonathan Wood On BEST ANSWER

While I may have had a few issues going on here, the following quote from Walkthrough: Creating and Registering a Custom HTTP Module seems to illuminate the problem:

If the ASP.NET application is running under IIS 6.0, you can use HTTP modules to customize requests for resources that are serviced by ASP.NET. This includes ASP.NET Web pages (.aspx files), Web services (.asmx files), ASP.NET handlers (.ashx files), and any file types that you have mapped to ASP.NET. If the ASP.NET application is running under IIS 7.0, you can use HTTP modules to customize requests for any resources that are served by IIS. This includes not just ASP.NET resources, but HTML files (.htm or .html files), graphics files, and so on.

As I was beginning to suspect, this is a change in ASP.NET 4.0 and IIS7. As I've mentioned several times, these are becoming more and more integrated. And the quote above suggests that HTTP modules now handle all files types served by IIS.

So that's why the older code doesn't work for me. And the fix appears to be to simply check the file type type from my handler.

1
Peter T. LaComb Jr. On

IIS 7 supports compression natively - there should not be any coding required. Unless, perhaps, you are trying to implement it on a hosting provider that has not enabled it, but that is not made clear in your question