可以在 ASP.NET/IIS 7 中选择性地禁用 gzip 压缩吗? [英] Can gzip compression be selectively disabled in ASP.NET/IIS 7?

查看:21
本文介绍了可以在 ASP.NET/IIS 7 中选择性地禁用 gzip 压缩吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用长期异步 HTTP 连接通过 AJAX 向客户端发送进度更新.启用压缩后,不会以离散块的形式接收更新(出于显而易见的原因).禁用压缩(通过将 元素添加到 )确实解决了问题:

I am using a long-lived asynchronous HTTP connection to send progress updates to a client via AJAX. When compression is enabled, the updates are not received in discrete chunks (for obvious reasons). Disabling compression (by adding a <urlCompression> element to <system.webServier>) does solve the problem:

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

但是,这会在站点范围内禁用压缩.除了这个控制器和/或动作之外,我想为每个其他控制器和/或动作保留压缩.这可能吗?或者我将不得不使用自己的 web.config 创建一个新站点/区域?欢迎提出任何建议.

However, this disables compression site-wide. I would like to preserve compression for every other controller and/or action except for this one. Is this possible? Or am I going to have to create a new site/area with its own web.config? Any suggestions welcome.

附言写入 HTTP 响应的代码是:

P.S. the code that does the writing to the HTTP response is:

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

推荐答案

@Aristos 的回答适用于 WebForms,但在他的帮助下,我调整了更符合 ASP.NET/MVC 方法的解决方案.

@Aristos' answer will work for WebForms, but with his help, I've adapted a solution more inline with ASP.NET/MVC methodology.

创建新过滤器以提供 gzip 压缩功能:

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 属性:

Create the NoGzip attribute:

public class NoGzipAttribute : Attribute {
}

使用 web.config 阻止 IIS7 进行 gzip 压缩:

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

在 Global.asax.cs 中注册您的全局过滤器:

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

最后,使用 NoGzip 属性:

Finally, consume the NoGzip attribute:

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

    public ActionResult GetProgressCompleted() 
    {
        ...
    }
}

附言再次感谢@Aristos 提出的有用的想法和解决方案.

P.S. Once again, many thanks to @Aristos, for his helpful idea and solution.

这篇关于可以在 ASP.NET/IIS 7 中选择性地禁用 gzip 压缩吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆