Locked use HttpCompression show unreadable characters

  • Sunday, December 05, 2010 11:06 AM
     
      Has Code

    I want to use httpcompression in may asp.net 3.5 website and IIS 7.5 Server,so I Create a ClassLibrary(HttpCompression) and write this codes:

    namespace Behsaman.Web
    {
    	public class HttpCompression : IHttpModule
    	{
    		#region IHttpModule Members
    
    		public void Dispose()
    		{
    		}
    
    		void IHttpModule.Init(HttpApplication context)
    		{
    			context.BeginRequest += new EventHandler(context_BeginRequest);
    		}
    
    		void context_BeginRequest(object sender, EventArgs e)
    		{
    
    			HttpApplication app = (HttpApplication)sender;
    
    			string encodings = app.Request.Headers.Get("Accept-Encoding");
    			if (!app.Request.RawUrl.Contains(".aspx") ||
    				HttpContext.Current.Request["HTTP_X_MICROSOFTAJAX"] != null ||
    				HttpContext.Current.Request["http_x-microsoftajax"] != null ||
    				HttpContext.Current.Items["hasBeenCompressed"] !=null ||
    				string.IsNullOrEmpty(encodings))
    				return;
    			encodings = encodings.ToLower();
    
          if (encodings.Contains("deflate"))
          {
            app.Response.Filter = new DeflateStream(app.Response.Filter, CompressionMode.Compress);
            app.Response.AppendHeader("Content-Encoding", "deflate");
          }
          else if (encodings.Contains("gzip"))
    			{
    
    				app.Response.Filter = new GZipStream(app.Response.Filter, CompressionMode.Compress);
    				app.Response.AppendHeader("Content-Encoding", "gzip");
    			}
          else
          {
            return;
          }
    			app.Response.Cache.VaryByHeaders["Accept-encoding"] = true;
    			HttpContext.Current.Items["hasBeenCompressed"] = true;
    		}
    
    		#endregion
    	}
    }


    and then add this dll to my website refrences. then I write this code in web.config

    # < system.webServer >  
    # ......... 
    #   < modules >  
    #    ........ 
    #    < add  name = "HttpCompression"  type = "Behsaman.Web.HttpCompression" />  
    #   </ modules >  
    #  ............. 
    #  </ system.webServer >


    when I browse a page I see unreadable characters like this:

    �`I�%&/m�{

    I see firebug and check Response Header :

    Cache-Control private
    Content-Type text/html; charset=utf-8
    Content-Encoding deflate,
    Server Microsoft-IIS/7.5
    X-AspNet-Version 2.0.50727
    X-Powered-By ASP.NET
    Date Sun, 05 Dec 2010 08:08:13 GMT
    Content-Length 62877
    In the Content-Encoding row you can see a comma after defalte word, is it normal?How can I fix my problem?