Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

ASP.NET WebResources and GZIP


:P
On this page:

 

I wonder if it’s possible to get WebResources.axd to serve GZIP’d content when you provide a resource that is already GZipped.

 

Yeah I know this sounds kind of harebrained, but I’m looking for a generic solution of compacting my JavaScript resource depending on a flag and serving a pre-created GZIP’d resource. My thought was I can GZip the JavaScript file, add it as a resource to the project and then at runtime determine which resource to ship back to the client.

 

I realize a more common approach is to use an HTTP module to accomplish this but until this is built into ASP.NET (not counting ATLAS) I would like to avoid having to add something to an installation.

 

Does anybody know if it’s possible to force a WebResource to not just set the ContentType but also the Content Encoding? That appears to be the only thing that’s keeping this scheme from working.

 

I can do this:

 

[assembly: WebResource("Westwind.Web.Controls.Resources.wwScriptLibrary.gzip",

                       "application/x-javascript")]

 

 

And I’ve tried:

 

[assembly: WebResource("Westwind.Web.Controls.Resources.wwScriptLibrary.gzip",

                       "application/x-javascript; content-encoding: gzip")]

 

But unfortunately that doesn’t work.

 

Building a GZIP Module is not terribly difficult given that GZIPStream is part of .NET 2.0 and that you can set a Response.Filter to this stream, but it would be just fine to not have to install a module and not take the encoding hit, especially based on the fact that WebResource.axd doesn’t seem to be very aggressive about caching.

 

Does anybody know if it's possible to coerce WebResource.axd to serve an already GZipped resource?

 


The Voices of Reason


 

Jason Haley
September 16, 2006

# Interesting Finds: September 16, 2006 PM Edition


Marc Brooks
September 17, 2006

# re: ASP.NET WebResources and GZIP

Looks like your best bet is to extend WebResource and the AssemblyResourceHandler to have these properties... it seems to be a glaring lack as it stands.

Bertrand Le Roy
September 17, 2006

# re: ASP.NET WebResources and GZIP

I'm sorry to say that I'm absolutely 100% positive you won't be able to do that without a module. We also wish it would be possible, believe me (and if somebody finds a way to do that, I'd go wow and be ultra-interested). You also need to factor in cacheability (which needs to depend on the accept-encoding header). Drop me an e-mail if you want to dicuss that further.

Ryan Heath
September 18, 2006

# re: ASP.NET WebResources and GZIP

Just today we discovered a bug in our code (or in asp.net) regarding cacheability and the accept-encoding header.

To make a long story short:
HttpCachePolicy policy = context.Response.Cache;
policy.SetCacheability(HttpCacheability.Public);
policy.SetNoServerCaching();

We added the last line today, it seems the caching wasnt looking at the accept-encoding header, so the first request was setting the encoding for all subsequent requests ...

Sending gzipped content while the accept-encoding header didnt specify it...

// Ryan

Rick Strahl
September 19, 2006

# re: ASP.NET WebResources and GZIP

Ryan, I think you can tweak the internal cache settings by using VaryByCustom and assigning the Request["Accept-Header"]. I was looking at a sample today that did just that.


Ryan Heath
September 21, 2006

# re: ASP.NET WebResources and GZIP

Aah, make sense!
// Ryan

# DotNetSlackers: ASP.NET WebResources and GZIP


Kariem Ali
October 16, 2008

# re: ASP.NET WebResources and GZIP

Hi Rick,

I am one of the developers behind Ra-Ajax, a Managed Ajax library for ASP.NET. Here's our take on this problem:

http://ra-ajax.org/webresource-axd-and-javascript-gzip-compression-a-simple-solution.blog

I hope it helps,

Kariem

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024