4

I use C# Asp.net 4, I have this script in Global.asax. I'm able to gzip all asp.net pages but NOT JavaScripts and CSS. I would like to know if is possible have compressed CSS and JS directly in my web app or it possible just in IIS. If is possible please point me out what I miss in my code. Thanks

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;
        string acceptEncoding = app.Request.Headers["Accept-Encoding"];
        Stream prevUncompressedStream = app.Response.Filter;

        if (!(app.Context.CurrentHandler is Page ||
            app.Context.CurrentHandler.GetType().Name == "SyncSessionlessHandler") ||
            app.Request["HTTP_X_MICROSOFTAJAX"] != null)
            return;

        if (acceptEncoding == null || acceptEncoding.Length == 0)
            return;

        acceptEncoding = acceptEncoding.ToLower();

        // if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
        if (acceptEncoding.Contains("gzip") || acceptEncoding == "*")
        {
            // gzip
            app.Response.Filter = new GZipStream(prevUncompressedStream,
                CompressionMode.Compress);
            app.Response.AppendHeader("Content-Encoding", "gzip");

        }
        // else if (acceptEncoding.Contains("gzip"))
        else if (acceptEncoding.Contains("deflate"))
        {
            // defalte
            app.Response.Filter = new DeflateStream(prevUncompressedStream,
            CompressionMode.Compress);
            app.Response.AppendHeader("Content-Encoding", "deflate");
        }
    }

4 Answers 4

1

Are you sure that *.css and *.js are handled by the ASP.NET runtime? They are not by default, you'd for example have to set the runAllManagedModulesForAllRequests to true in web.config.

Also, why don't you rely on the IIS built-in compression?

Sign up to request clarification or add additional context in comments.

2 Comments

IIS built-in compression is valid just for static content.. or even for dynamic page? if is the case shall I remove my script from the Global.asax? thanks
IIS 7 compresses both static and dynamic data. Sticking with built-in compression sounds like a better idea, imho.
1

Minify and gzip your CSS and JS ahead of time, also combine all the JS in one file and all CSS in one file:

Online JavaScript/CSS Compression Using YUI Compressor

If you search a little you will find all kinds of way to automate the compression and minification into a post-build process or CI build process.

1 Comment

I also fond microsoft-ajax-minifier could be useful stephenwalther.com/blog/archive/2009/10/16/…
0

You can and should set up IIS to compress static files. Let IIS handle it before it even gets to your app.

You can set this manually in IIS Management Console or use the <httpCompression> setting in your web.config.

2 Comments

thanks I did not know about <httpCompression>... would be possible also to stripping (minifying) the css and js directly in IIS?
do you mean? <system.webServer> <urlCompression doStaticCompression="true" doDynamicCompression="true"/> </system.webServer>
0

Yes, we can take the advantage of IIS 7.0 compression fearutres. Also you are really concerned about performance, it is always better to create one JS and one CSS in your applications, if this is not possible, it is better to merge those JS and CCS into one Js and CSS. And also it is always recommended to us AJAX Minifier.

http://www.mindfiresolutions.com/How-to-merge-your-Javascript-files-in-ASPNET-1221.php

http://www.mindfiresolutions.com/How-to-merge-CSS-files-in-ASPNET-1230.php

Please feel free to contact me if you need further help in this.

Thanks, Himanshu Shekhar Sahu www.mindfiresolutions.com

3 Comments

Thanks for your comment. so would you advice me to remove my code from the Global.asax file and instead using II7 features for compressing both static and dynamic content?
Yes, if you are using IIS 7/7.5 then absolutely no need. Please go through below links to get more idea on this.
@Gibbok Yes, if you are using IIS 7/7.5 then absolutely no need. Please go through below links to get more idea on this. weblogs.asp.net/owscott/archive/2009/02/22/… . If you are using any .axad and to strip out white spaces in the files, it is recommended to create one HTTPHandler to handle those.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.