2012-01-10 68 views
1

我用下面的代码来缩小我在飞行中的CSS:修改CSS文件(缩小)使用的HttpModule:GZip压缩CRC校验失败

namespace MyCMS.Modules 
{ 
    public class CSSModule : IHttpModule 
    { 
     void IHttpModule.Dispose() 
     { 
      throw new NotImplementedException(); 
     } 

     void IHttpModule.Init(HttpApplication context) 
     { 
      context.BeginRequest += new EventHandler(context_BeginRequest); 
     } 

     void context_BeginRequest(object sender, EventArgs e) 
     { 
      HttpApplication app = (HttpApplication)sender; 
      HttpContext context = app.Context; 

      if (app.Request.RawUrl.Contains(".css")) 
      { 
       context.Response.Filter = new CSSFilter(app.Response.Filter); 
      } 
     } 

     private class CSSFilter : Stream 
     { 
      public CSSFilter(Stream sink) { _sink = sink; } 
      private Stream _sink; 

      public override bool CanRead { get { return true; } } 
      public override bool CanSeek { get { return true; } } 
      public override bool CanWrite { get { return true; } } 
      public override void Flush() { _sink.Flush(); } 
      public override long Length { get { return 0; } } 
      private long _position; 
      public override long Position 
      { 
       get { return _position; } 
       set { _position = value; } 
      } 

      public override int Read(byte[] buffer, int offset, int count) { return _sink.Read(buffer, offset, count); } 
      public override long Seek(long offset, SeekOrigin origin) { return _sink.Seek(offset, origin); } 
      public override void SetLength(long value) { _sink.SetLength(value); } 
      public override void Close() { _sink.Close(); } 
      public override void Write(byte[] buffer, int offset, int count) 
      { 
       byte[] data = new byte[count]; 
       Buffer.BlockCopy(buffer, offset, data, 0, count); 
       string html = System.Text.Encoding.Default.GetString(buffer); 

       html = Yahoo.Yui.Compressor.CssCompressor.Compress(html,0, Yahoo.Yui.Compressor.CssCompressionType.Hybrid, true); 

       byte[] outdata = System.Text.Encoding.Default.GetBytes(html); 
       _sink.Write(outdata, 0, outdata.GetLength(0)); 
      } 
     } 
    } 
} 

的问题上,gzip的CRC(已启用在服务器上)失败。 我明白为什么它失败,因为文件内容是X和现在它的Y(缩小), 和原始的CRC计算为X而不是Y

我能做些什么来解决这个问题?

回答

0

我已经创建了CSSModule样本MVC应用程序,

随着出模块的site.css大小为6KB〜。

使用模块Site.css大小〜1kb。

可能会帮助你。

链接:http://www.mediafire.com/?a86fqg1zqy44ssc

+0

我不使用WebResource.axd的,也不会考虑使用它的,我给的CSS模块为例,但我需要重写一些滤波器输出文件,这就是为什么我发布的问题。 – Dementic 2012-01-11 08:39:18