ASP.NET Core压缩中间件-空响应 [英] ASP.NET Core Compression Middleware - Empty Reponse

查看:116
本文介绍了ASP.NET Core压缩中间件-空响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此存储库中的一些自定义压缩中间件(粘贴在下面).根据第一个请求,内容将被压缩.对于此后的每个请求,响应都将返回为完全空(Content-Length为0).

I am using some custom compression middleware from this repository (pasted below). Upon the first request, the content is compressed just fine. For every request after that, the response comes back as completely empty (with a Content-Length of 0).

这只有在从ASP.NET Core RC2迁移到RTM之后才开始发生.

This only started happening after migrating from ASP.NET Core RC2 to RTM.

有人知道为什么会这样吗?

Does anyone know why this is happening?

CompressionMiddleware:

public class CompressionMiddleware
{
    private readonly RequestDelegate _next;

    public CompressionMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        if (IsGZipSupported(context))
        {
            string acceptEncoding = context.Request.Headers["Accept-Encoding"];

            var buffer = new MemoryStream();
            var stream = context.Response.Body;
            context.Response.Body = buffer;
            await _next(context);

            if (acceptEncoding.Contains("gzip"))
            {
                var gstream = new GZipStream(stream, CompressionLevel.Optimal);
                context.Response.Headers.Add("Content-Encoding", new[] { "gzip" });
                buffer.Seek(0, SeekOrigin.Begin);
                await buffer.CopyToAsync(gstream);
                gstream.Dispose();
            }
            else
            {
                var gstream = new DeflateStream(stream, CompressionLevel.Optimal);
                context.Response.Headers.Add("Content-Encoding", new[] { "deflate" });
                buffer.Seek(0, SeekOrigin.Begin);
                await buffer.CopyToAsync(gstream);
                gstream.Dispose();
            }
        }
        else
        {
            await _next(context);
        }
    }

    public bool IsGZipSupported(HttpContext context)
    {
        string acceptEncoding = context.Request.Headers["Accept-Encoding"];
        return !string.IsNullOrEmpty(acceptEncoding) &&
               (acceptEncoding.Contains("gzip") || acceptEncoding.Contains("deflate"));
    }
}

推荐答案

我在"

我添加了gzip,它可以正常工作,但是第一个请求.我的意思是在第一个请求中,响应页面为null(context.Response.Body),但是当您刷新页面(仅一次)后,它便可以正常工作了(我不知道为什么,但我必须解决它)/em>

I have added gzip and it worked, but first request. I mean in the first request, the response page is null (context.Response.Body) but when you refresh the page (just once) it works correctly after that.(I don't know why but I have to solve it)

问题的答案是:

您需要更新 实际压缩的context.Response.Headers ["Content-Length"] 缓冲区长度.

You need to update context.Response.Headers["Content-Length"] with actual compressed buffer length.

CompressionMiddleware.cs

上面压缩中间件的实现链接包含:

And above link to realisation of compression middleware contains:

if (context.Response.Headers["Content-Length"].Count > 0)
{
   context.Response.Headers["Content-Length"] = compressed.Length.ToString();
}

这篇关于ASP.NET Core压缩中间件-空响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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