2011-04-13 11 views
10

AJAX aracılığıyla bir istemciye ilerleme güncellemeleri göndermek için uzun ömürlü bir asenkron HTTP bağlantısı kullanıyorum. Sıkıştırma etkinleştirildiğinde, güncellemeler ayrık parçalarda (açık nedenlerden dolayı) alınmaz.Sıkıştırma ASP.NET/IIS 7'de seçici olarak devre dışı bırakılabilir mi?

<urlCompression doStaticCompression="true" doDynamicCompression="false" /> 

Ancak bu devre dışı bırakır sıkıştırma sitesini çapında: (<system.webServier> bir <urlCompression> öğesi ekleyerek) sıkıştırma devre dışı bırakılması sorunu çözmek gelmez. Bunun dışındaki tüm diğer denetleyici ve/veya eylemler için sıkıştırmayı korumak istiyorum. Mümkün mü? Yoksa kendi web.config ile yeni bir site/alan oluşturmak zorunda mıyım? Herhangi bir öneri hoşgeldiniz.

P.S. HTTP yanıtı yazma yapar kod şudur:

var response = HttpContext.Response; 
response.Write(s); 
response.Flush(); 

cevap

12

@Aristos' cevabı WebForms için çalışacak, ama onun yardımıyla, ben ASP.NET/MVC metodolojisi ile daha inline bir çözüm adapte ettik. kullanarak web.config Gzip Sıkıştırma gelen

public class NoGzipAttribute : Attribute { 
} 

önle IIS7:

public class GzipFilter : ActionFilterAttribute 
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     base.OnActionExecuted(filterContext); 

     var context = filterContext.HttpContext; 
     if (filterContext.Exception == null && 
      context.Response.Filter != null && 
      !filterContext.ActionDescriptor.IsDefined(typeof(NoGzipAttribute), true)) 
     { 
      string acceptEncoding = context.Request.Headers["Accept-Encoding"].ToLower();; 

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

NoGzip özelliği oluşturma:

Gzip Sıkıştırma işlevsellik sağlamak için yeni filtre oluşturun
<system.webServer> 
    ... 
    <urlCompression doStaticCompression="true" doDynamicCompression="false" /> 
</system.webServer> 

Global.asax.cs da küresel filtreyi Kayıt:

public class MyController : AsyncController 
{ 
    [NoGzip] 
    [NoAsyncTimeout] 
    public void GetProgress(int id) 
    { 
     AsyncManager.OutstandingOperations.Increment(); 
     ... 
    } 

    public ActionResult GetProgressCompleted() 
    { 
     ... 
    } 
} 

Dip not:

protected void Application_Start() 
{ 
    ... 
    GlobalFilters.Filters.Add(new GzipFilter()); 
} 
Nihayet

, NoGzip niteliğini tüketmek Bir kez daha, yardımcı fikir ve çözüm için çok teşekkürler @Aristos.

3

Ne istediğinizde size kendini, selectivle tarafından gzip sıkıştırmasını göndersek? Yapmak istediğinizde ve sıkıştırma yapmadığınız zaman Application_BeginRequest kontrol edin. İşte örnek bir kod.

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    string cTheFile = HttpContext.Current.Request.Path; 
    string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile); 

    if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase)) 
    { 
     string acceptEncoding = MyCurrentContent.Request.Headers["Accept-Encoding"].ToLower();; 

     if (acceptEncoding.Contains("deflate") || acceptEncoding == "*") 
     { 
      // defalte 
      HttpContext.Current.Response.Filter = new DeflateStream(prevUncompressedStream, 
       CompressionMode.Compress); 
      HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate"); 
     } else if (acceptEncoding.Contains("gzip")) 
     { 
      // gzip 
      HttpContext.Current.Response.Filter = new GZipStream(prevUncompressedStream, 
       CompressionMode.Compress); 
      HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip"); 
     }  
    } 
} 
+0

teşekkürler. Cevabımda anlattığım çözümü yaratmak için rehberliğini kullandım. –

4

Bunu yapmanın daha kolay bir yolunu buldum. Kendi sıkıştırmanızı seçerek yapmak yerine, varsayılan IIS sıkıştırmasını seçerek devre dışı bırakabilirsiniz (web.config dosyasında etkinleştirilmiş olduğu varsayılarak).

Yalnızca istek üzerine kodlama kodlama üstbilgisini kaldırın ve IIS sayfayı sıkıştırmayacaktır.

(global.asax.cs :) Yardımlarınız için

protected void Application_BeginRequest(object sender, EventArgs e) 
{ 
    try 
    { 
     HttpContext.Current.Request.Headers["Accept-Encoding"] = ""; 
    } 
    catch(Exception){} 
} 
+1

* Sunucu * tarafındaki * request * başlıklarını değiştirmek son derece kötü bir uygulamadır. Sunucu tarafı davranışını değiştirmek için istek başlıklarını oluşturmak için sunucuda hackleme yapıyorsunuz. Bu en azını söylemek için çok üzücü. –

İlgili konular