2011-04-13 10 views
10

मैं AJAX के माध्यम से किसी क्लाइंट को प्रगति अद्यतन भेजने के लिए एक दीर्घकालिक एसिंक्रोनस HTTP कनेक्शन का उपयोग कर रहा हूं। जब संपीड़न सक्षम होता है, तो अद्यतन अलग-अलग हिस्सों में स्पष्ट नहीं होते हैं (स्पष्ट कारणों से)।एएसपी.NET/IIS 7 में संपीड़न को चुनिंदा रूप से अक्षम किया जा सकता है?

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

हालांकि, इस को निष्क्रिय संपीड़न साइट-व्यापी: (<system.webServier> करने के लिए एक <urlCompression> तत्व जोड़कर) संपीड़न को अक्षम करने समस्या को हल करता है। मैं इस के अलावा हर दूसरे नियंत्रक और/या कार्रवाई के लिए संपीड़न को संरक्षित करना चाहता हूं। क्या यह संभव है? या क्या मुझे अपनी खुद की web.config के साथ एक नई साइट/क्षेत्र बनाना होगा? कोई सुझाव स्वागत है।

पीएस कोड है कि HTTP प्रतिसाद करने के लिए लेखन करता है:

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

उत्तर

12

@Aristos 'जवाब WebForms के लिए काम करेंगे, लेकिन उसकी मदद के साथ, मैं ASP.NET/MVC पद्धति से अधिक इनलाइन एक समाधान अनुकूलित है। web.config का उपयोग कर gzipping से

public class NoGzipAttribute : Attribute { 
} 

रोकें 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 विशेषता बनाएँ:

gzipping सुविधा प्रदान करने के एक नया फ़िल्टर बनाएं

<system.webServer> ... <urlCompression doStaticCompression="true" doDynamicCompression="false" /> </system.webServer> 

Global.asax.cs में अपने वैश्विक फिल्टर रजिस्टर:

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

    public ActionResult GetProgressCompleted() 
    { 
     ... 
    } 
} 

पी.एस.:

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

अंत में, NoGzip विशेषता का उपभोग एक बार फिर, उनके सहायक विचार और समाधान के लिए @ एरिस्टोस के लिए बहुत धन्यवाद।

3

क्या आप अपने आप, selectivle द्वारा gzip संपीड़न सेट के बारे में जब आप के लिए करना चाहते हैं? जब आप बनाना चाहते हैं और जब आप संपीड़न नहीं करते हैं तो Application_BeginRequest चेक पर जांचें। यहां एक नमूना कोड है।

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

आपकी मदद के लिए धन्यवाद। मैंने अपने उत्तर में दिए गए समाधान को बनाने के लिए आपके मार्गदर्शन का उपयोग किया है। –

4

मुझे ऐसा करने का एक आसान तरीका मिला। अपने स्वयं के संपीड़न को चुनिंदा रूप से करने के बजाय, आप चुनिंदा आईआईएस संपीड़न को चुन सकते हैं (इसे अपने web.config में सक्षम मानते हैं)।

अनुरोध पर स्वीकृति-एन्कोडिंग एन्कोडिंग हेडर को हटा दें और आईआईएस पृष्ठ को संपीड़ित नहीं करेगा।

(global.asax.cs :)

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

* सर्वर * पक्ष पर * अनुरोध * हेडर को संशोधित करना बेहद खराब अभ्यास है। अनुरोध सर्वर हेडर को सर्वर-साइड व्यवहार को संशोधित करने के लिए आप सर्वर पर मूल रूप से हैकिंग कर रहे हैं। कम से कम कहने के लिए यह भरा हुआ है। –

संबंधित मुद्दे

 संबंधित मुद्दे