0

I am implementing HttpModule for compressing request. Below is the codee for HttpModule:

public class Global : IHttpModule
{
public void Init(HttpApplication app)
{
    app.PostReleaseRequestState += new EventHandler(app_PostReleaseRequestState);
}
void app_PostReleaseRequestState(object sender, EventArgs e)
{
    HttpApplication app = (HttpApplication)sender;
    HttpContext context = app.Context;
    string acceptEncoding = context.Request.Headers["Accept-Encoding"];




        // If gzip is supported then gzip it else if deflate compression is supported then compress in that technique.
        if (acceptEncoding.Contains("gzip"))
        {
            // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
            context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
            context.Response.AppendHeader("Content-Encoding", "gzip");
        }
        else if (acceptEncoding.Contains("deflate"))
        {
            // Compress and set Content-Encoding header for the browser to indicate that the document is zipped.
            context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
            context.Response.AppendHeader("Content-Encoding", "deflate");
        }


}

It's able to intercept and compress js and css in the development web server but when i run it from IIS 5.1 it is not able to compress js and css files. Please help.

Niraj Choubey
  • 3,942
  • 18
  • 58
  • 93

3 Answers3

2

Prior to IIS7, getting non-ASP.NET file types into the ASP.NET pipeline requires setting up a file type mapping to redirect those files through ISAPI. If you map *.js and *.css to be processed by ISAPI, your code should start running for those requests.

Here's an example of doing that in IIS6 (though you'll want to substitute *.js and *.css for *.asp). If I remember correctly, 5.1's management interface is similar enough that ScottGu's example should still be helpful.

Dave Ward
  • 59,815
  • 13
  • 117
  • 134
  • do we need to write HttpHandler for this or just by add file type it cwill work. – Niraj Choubey Feb 14 '11 at 14:33
  • Just add the file type. Files of those types will start running through your HttpModules then. – Dave Ward Feb 14 '11 at 14:37
  • Make sure you're routing those extensions to ISAPI. If nothing else, consider trying a "wildcard" mapping, by changing the existing `*.aspx` mapping to `*.*`. That way you can be sure you've got the correct handler assigned. – Dave Ward Feb 14 '11 at 19:29
1

I would make sure that .js and .css files are handled by the .NET framework.

The reference for IIS 7 and above can be found at iis.net/ConfigReference/system.webServer/handlers

Concerning IIS 6, you can check that js and css are handled under: Site settings / Home Directory / Application Settings / (Application Pool) Configuration / Mappings

E. Jaep
  • 2,095
  • 1
  • 30
  • 56
0

You are on the right track using HttpModule. However, in order to change HTML contents, you would use the HttpApplication.PostReleaseRequestState handler. In order to change resource files, you would use the HttpApplication.BeginRequest handler.

L Tran
  • 11
  • 1