Click here to Skip to main content
15,897,519 members
Articles / Web Development / HTML

Minify contents - Reduce page size

Rate me:
Please Sign up or sign in to vote.
4.75/5 (4 votes)
19 Jul 2012CPOL2 min read 31.3K   1.8K   12  
A library to minify HTML and CSS content.
using System;
using System.Reflection;
using System.Web;

namespace MinifyContent
{
    class ContentCompresser : IHttpModule
    {

        static bool? isDebugMode;

        public void Dispose()
        {}

        public void Init(HttpApplication context)
        {
            //Handle Post Release Request to handle page output
            context.PostReleaseRequestState += new EventHandler(context_PostReleaseRequestState);
        }

        protected void context_PostReleaseRequestState(object sender, EventArgs e)
        {

            HttpApplication app = (HttpApplication)sender;
            HttpContext context = app.Context;

            //set isDebugMode if its value is null
            if (isDebugMode == null)
            { 
                Assembly assembly = AssemblyGen.GetWebApplicationAssembly(context);
                isDebugMode = AssemblyGen.IsDebugMode(assembly);
            }
            //Check that running web assembly is not in debug mode and also check that Content type should be "text/html" or "text/css"
            /* Two If Conditions are here, first is in use and second is commented. 
             * For published mode use first condition. This will make assurance that your deployment has been done in publish mode.
             * To verify At development time for unit testing purpose use second condition in debug & release mode.
             */
            if (!isDebugMode.Value && !context.IsDebuggingEnabled && (context.Response.ContentType == "text/html" || context.Response.ContentType == "text/css"))
            //if (!isDebugMode.Value && (context.Response.ContentType == "text/html" || context.Response.ContentType == "text/css"))
                context.Response.Filter = new ResponseStream(context.Response);

        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Mehul Thakkar is having 8 yrs of experience in IT industry. He is having good command over Ms .Net and Ms Sql Server

Comments and Discussions