是否可以在ASP.NET/IIS 7中有选择地禁用gzip压缩?

| 我正在使用一个长期存在的异步HTTP连接,以通过AJAX将进度更新发送给客户端。启用压缩后,不会以离散的块形式接收更新(出于明显的原因)。禁用压缩(通过向
<system.webServier>
添加
<urlCompression>
元素)可以解决此问题:
<urlCompression doStaticCompression=\"true\" doDynamicCompression=\"false\" />
但是,这将禁用站点范围的压缩。我想保留除此以外的所有其他控制器和/或动作的压缩。这可能吗?还是我必须使用自己的web.config创建一个新站点/区域?任何建议欢迎。 附言编写HTTP响应的代码是:
var response = HttpContext.Response;
response.Write(s);
response.Flush();
    
已邀请:
@Aristos的答案将适用于WebForms,但是在他的帮助下,我已经改编了一种更符合ASP.NET/MVC方法的解决方案。 创建一个新的过滤器以提供gzipping功能:
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\");
            } 
        }
    }
}
创建“ 5”属性:
public class NoGzipAttribute : Attribute {
}
使用web.config阻止IIS7压缩:
<system.webServer>
    ...
    <urlCompression doStaticCompression=\"true\" doDynamicCompression=\"false\" />
</system.webServer>
在Global.asax.cs中注册全局过滤器:
protected void Application_Start()
{
    ...
    GlobalFilters.Filters.Add(new GzipFilter());
}
最后,使用
NoGzip
属性:
public class MyController : AsyncController
{
    [NoGzip]
    [NoAsyncTimeout]
    public void GetProgress(int id)
    {
        AsyncManager.OutstandingOperations.Increment();
        ...
    }

    public ActionResult GetProgressCompleted() 
    {
        ...
    }
}
附言再次感谢@Aristos的有益想法和解决方案。     
我发现了一种更简单的方法。可以有选择地禁用默认的IIS压缩,而不是有选择地进行自己的压缩(假设在web.config中启用了它)。 只需删除请求上的accept-encoding编码标头,IIS便不会压缩页面。 (global.asax.cs :)
protected void Application_BeginRequest(object sender, EventArgs e)
{
    try
    {
        HttpContext.Current.Request.Headers[\"Accept-Encoding\"] = \"\";
    }
    catch(Exception){}
}
    
您如何根据自己的需要设置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\");
        }       
    }
}
    

要回复问题请先登录注册