Link to home
Start Free TrialLog in
Avatar of ARACK04
ARACK04

asked on

IHttpModule not getting called

Put simply, is there any reason why an IHttpModule would not get called for requests for script files?  My module is set to gzip compress all JSON responses, and also all script files.  It works perfectly locally, but when I deploy it to my shared hosting account, it **only works for the JSON responses**.

Is there some IIS setting that they might have to cause requests for these files to bypass my HttpModule somehow?

Thanks!
Avatar of ARACK04
ARACK04

ASKER

Also, here's the complete code for my module:
using System;
using System.IO;
using System.IO.Compression;
using System.Globalization;
using System.Web;
 
 
public class JsonCompressionModule : IHttpModule {
    public JsonCompressionModule() {
    }
 
    public void Dispose() {
    }
 
    public void Init(HttpApplication app) {
        app.PreRequestHandlerExecute += new EventHandler(Compress);
    }
 
    private void Compress(object sender, EventArgs e) {
        HttpApplication app = (HttpApplication)sender;
        HttpRequest request = app.Request;
        HttpResponse response = app.Response;
 
        if (request.ContentType.ToLower(CultureInfo.InvariantCulture).StartsWith("application/json") ||
            request.Url.ToString().Contains(".js")) {
 
            if (!((request.Browser.IsBrowser("IE")) && (request.Browser.MajorVersion <= 6))) {
                string acceptEncoding = request.Headers["Accept-Encoding"];
 
                if (!string.IsNullOrEmpty(acceptEncoding)) {
                    acceptEncoding = acceptEncoding.ToLower(CultureInfo.InvariantCulture);
 
                    if (acceptEncoding.Contains("gzip")) {
                        response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
                        response.AddHeader("Content-encoding", "gzip");
                    } else if (acceptEncoding.Contains("deflate")) {
                        response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
                        response.AddHeader("Content-encoding", "deflate");
                    }
                }
            }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This window in IIS controls what file extensions map to what, you can add to these as you wish.
IIS.gif
Avatar of ARACK04

ASKER

Genius, thanks!

Why did it work in localhost though?  Are requests through localhost not capable of knowing what's static, and just send everything through the ASP.NET runtime?
Avatar of ARACK04

ASKER

Instead of .js.aspx, couldn't I just add .js to the extensions handled by .NET directly?  Also, if I did that, would it still work for requests with a querystring added on, like

foo.js?ver=1.6    

I would assume so since requests like that still work for .aspx files.

Thanks for putting up with this simple (probably stupid) question!
It works on localhost because the development server in visual studio routes all traffic through the runtime.

You could indeed map .js to asp.net and handle them there.