On the server side, you can compress your answers using the HttpModule . The HttpModule is similar to global.asax in that it provides lifecycle event requests and responses such as BeginRequest, ReleaseRequestState, and EndRequest. You can change the response content by processing the corresponding event and changing the output stream. Usually you modify the response by adding an output filter . For example, the HttpCompress module (no longer updated) attaches compression filters in ReleaseRequestState or PreSendRequestHeaders events:
http://code.google.com/p/httpcompress/source/browse/trunk/HttpCompress/HttpModule.cs
In addition to compressing the content, you need to set the Content-Encoding header. For a more recent (though beta) example, check out the Rich Crane Wicked Compression module. CodeProject also has an example .
Keep in mind that a compressed HttpModule will most likely not play well if IIS is configured for server-level compression. Also, be prepared for a few corner things if you use Ajax or .axd handlers. These requests may not work as you expect, or may not go through your module without explicitly passing them.
On the client side, you need to transfer your custom Accept-Encoding token with each request. Avoid compression on the server if this token is not installed. If you add clients in the future, it may not be clear why they fail if all responses are compressed with your custom compression.
I'm not sure how your clients make requests, but most Http Request / Response pipelines allow you to use a response stream similar to a server. Before a response is sent to your render, check your custom Content-Encoding token. If present, start the decompression procedure. If not, pass the answer unchanged.
source share