开发者

ASP.NET gzip script and CSS on Global.asax

开发者 https://www.devze.com 2022-12-19 01:39 出处:网络
I am implementing this library in my application, GZip compress your website\'s HTM开发者_如何学编程L/CSS/Script in code.

I am implementing this library in my application, GZip compress your website's HTM开发者_如何学编程L/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 JavaScript files.

Is there a better way for implementing JavaScript and CSS gzip in C# corresponding to Visual Studio 2005 (changing the IIS is not an option as it has to be in the code).


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消