views:

382

answers:

1

Hello i implement this library in my application

http://www.dominicpettifer.co.uk/Blog/17/gzip-compress-your-websites-html-css-script-in-code

it Works very well if i run the site in Visual Studio but when i compile my site and publish in IIS it only Gzip ASPX files not CSS or JS files.

does anyone knows a better way for implement JavaScript and CSS Gzip in C# 2005 (changing the IIS its not an option it has to be in the code)

thanks in advance

A: 

First, you need to create an HttpHandler for processing it:

namespace com.waynehartman.util.web.handlers
{
    [WebService(Namespace = "http://waynehartman.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class HttpCompressor : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            // Code for compressing the file and writing it to the HttpResponse
        }
        public bool IsReusable
        {
            get { return true; }
        }
    }
}

Then you need to add a handler mapping in your web.config:

<configuration>
    <system.web>
        <httpHandlers>
            <add verb="*" path="*.css" 
                type="com.waynehartman.util.web.handlers.HttpCompressor, WayneHartmanUtilitiesLibrary"/>
            <add verb="*" path="*.js" 
                type="com.waynehartman.util.web.handlers.HttpCompressor, WayneHartmanUtilitiesLibrary"/>
        </httpHandlers>
    </system.web>
</configuration>
Wayne Hartman
and FullyQualified.Class and LibraryName will be?
jmpena
as far as i know HttpHandlers is to combine multiple files in one, or something like this.
jmpena
@jmpena HttpHandlers are for creating any customized handling of an http request or response. They are designed to allow you to do exactly what you are trying to do.
Wayne Hartman
ok ill try your solution, but just one question (im a win32 dev), the code u put its a webservice or can be a class in my website ?
jmpena
@jmpena [WebService] tags appear there by default when you create a new Generic Handler through Visual Studio. I'm not sure what the side affects would be by removing them, by they are not hurting you by being there. Try to think of a http handler as a rudimentary and nonspecializing aspx page. Instead of .NET doing all the handling for a page request for you, you have complete control over what the request/response is.
Wayne Hartman