2012-02-24 85 views
1

我添加了一个名为Application_PreRequestHandlerExecute在global.ascx这样的方法:如何在代码级别压缩asp.net页面?

void Application_PreRequestHandlerExecute(object sender, EventArgs e) 
{ 

    string cTheFile = HttpContext.Current.Request.Path; 
    string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile); 

    if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase)) 
    { 
     HttpApplication httpApp = (HttpApplication)sender; 

     string acceptEncoding = httpApp.Request.Headers["Accept-Encoding"]; 
     if (string.IsNullOrEmpty(acceptEncoding)) 
     { 
      return; 
     } 
     acceptEncoding = acceptEncoding.ToLower(); 

     System.IO.Stream requestStream = httpApp.Response.Filter; 

     if (acceptEncoding.Contains("gzip")) 
     { 
      httpApp.Response.Filter = new System.IO.Compression.GZipStream(requestStream, 
       System.IO.Compression.CompressionMode.Compress); 
      httpApp.Response.AppendHeader("Content-Encoding", "gzip"); 
     } 
     else if (acceptEncoding.Contains("deflate")) 
     { 
      httpApp.Response.Filter = new System.IO.Compression.DeflateStream(requestStream, 
       System.IO.Compression.CompressionMode.Compress); 
      httpApp.Response.AppendHeader("Content-Encoding", "deflate"); 
     } 
    } 

} 

它工作时,浏览普通页面。

但是如果页面包含UPDATE-PANEL错误会发生。 我得到一个PageRequestParserException。 当更新面板异步回发时,会发生此错误。

有什么想法吗?

+1

你能请telll我错误 – 2012-02-24 09:12:19

+0

@SanjayGoswami PageRequestParserException – 2012-02-24 09:45:30

+0

@SanjayGoswami PageRequestParserException时更新面板回发碰巧请求 – 2012-02-24 09:57:55

回答

0

我通过在我的页面上将EnableEventValidation设置为false并将压缩逻辑移动到页面的构造函数来“固定”。显然这不是一个好的解决方案(关闭验证)。 如果有人知道一个好的解决方案,请让我知道。

并发现如果该项目的框架版本是3.5,所有工作正常,但如果该版本是2.0。这个错误会发生。