IIS 7 and Above
How to setup GZIP using a HTTPModule?
Last post Apr 29, 2010 10:04 AM by vivekj
Dec 26, 2009 05:01 PM|JerryVRC|LINK
I am on a IIS 7.0 Windows 2008 server in a shared hosting environment. I would like to enable gzipping of my .html files but I cannot use the default static file compression feature because of the shared hosting environment. The webhost provided the following
The only way to enable the headers to pickup the compressions enabled setting in our enviornment is via creating your own headers for any and all HttpApplication.Begin_Request event, since we do not modify the servers due to the nature of the shared environment.
With that said, you will need to create the headers on your code using the HttpContext.Current.Response.AppendHeader event handler.
I was able to find the following commentary on the web but I am unsure of what to do next:
If you don't have access to the final IIS instance (shared hosting...) you can create a HttpModule that adds this code to every HttpApplication.Begin_Request event :
I am have some familiarity with the IIS Manager, having used it to write a web.config file for URL Rewrite. Any guidance or assistance would be sincerely appreciated.
Dec 29, 2009 05:12 AM|Leo Tang - MSFT|LINK
With IIS7.0 integrate mode, you can achieve this by using a custom module. The code looks like:
You can save the above code as CompressModule.cs file, copy the file to your site's bin folder, then use the following section enable this module:
<add name="Compress" type="CompressModule" />
For more information, please refer to:
Developing IIS 7.0 Modules and Handlers with the .NET Framework
Dec 29, 2009 08:49 AM|JerryVRC|LINK
Thanks for the above information. However, I don't have access to system.webServer since this is shared hosting environment. I assume I have to use the web.config file somehow instead. What are the steps necessary to do this using IIS 7.0 Manager?
Dec 29, 2009 04:19 PM|Jamescoo|LINK
In Inetmgr you can add modules at the site level as well through web.config with an entry like ( just an example ).. does this help?
Jan 01, 2010 01:04 PM|JerryVRC|LINK
OK, I think I understand to a limited extent, as follows:
1. Copy the code provided by Leo Tang (into Notepad? should the code be indented as a I see in other examples, and, if so, how do i do it, or doesn't that matter?) and save the file as CompressModule.cs
2. Copy the file into the site's bin folder -- How do I do this (copy and paste?) and where do find the bin folder? or is this a folder I create, and, if so, how and where?
3. Once 1 and 2 are done. use IIS Manager to add the module as a managed module via the web.config file.
Thanks in advance for your assistance.
Jan 04, 2010 03:05 AM|Leo Tang - MSFT|LINK
1. You can copy the code into Notepad without code indented, insure the file was saved as a file with .cs extension. It is recommended you write a custom module by using Visual Studio.
2. Sorry, my mistake. As the code file hasn't been compiled to .dll assembly, it should be copied to the "App_Code" folder in your site/application root. If the "App_Code" folder doesn't exists, you can manually create it.
3. You can add the module by using graphic user interface(IIS manager->High-light your site/application on the connection pane->Click the Modules icon on the home pane->Click Add Manage Module.. on the Actions pane). You can also directly edit the web.config
file to add this module.
You can find some more detailed information about develop custom module in the following reference, hope helps:
Jan 05, 2010 06:03 PM|JerryVRC|LINK
Thanks for the above information. However, after checking into things further, I found that the shared hosting environment I'm in (Web.com) is operated in the classic mode rather than the integrated mode. While I can switch to the integrated mode, I'll
do so at my own risk, which isn't necessarily a wise thing to do in an environment where I don't actually have much control or support. How does operating in the classic mode change your advice above?
Jan 06, 2010 05:50 AM|Leo Tang - MSFT|LINK
With classic mode, you can use following configuration section add the module(still copy CompressModule.cs file to the App_Code folder).
As in Classic Mode, the httpModule won't invoke for the request to html file. You need to map the *.html to %windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll(64bit) or %windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll(32bit). The
configuration section looks like:
Apr 28, 2010 02:29 PM|vivekj|LINK
This looks like something i could use. I am trying to serve up compressed aspx/css/js files from a shared hosting server. What would i need to add to the web.config file for these formats?
Apr 29, 2010 08:19 AM|Leo Tang - MSFT|LINK
What is your IIS version? Do you want to use integrated mode or classic mode?
You can send an email to me at: liangliang(dot)tang(at)hotmail(dot)com. I'll send the sample code to you.
Apr 29, 2010 10:04 AM|vivekj|LINK
Thanks Leo, email heading your way.