Gzip压缩中间件在ASP.NET Core 2.0中不起作用 [英] Gzip compression middleware not working in asp.net core 2.0

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

问题描述

我无法使Gzip压缩中间件在asp.net core 2.0(或2.1)中工作.我使用空白" Web应用程序模板创建了一个新项目,并在Startup.cs中包含以下代码:

I am unable to get Gzip compression middleware to work in asp.net core 2.0 (or 2.1). I created a new project using the "Blank" Web Application template and have the following code in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddResponseCompression();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseResponseCompression();

    app.Run(async (context) =>
        {
            context.Response.Headers.Add("Content-Type", "text/plain");
            await context.Response.WriteAsync("Hello World!");
        });
}

我确认Chrome浏览器在请求中发送了接受编码:gzip,deflate和br".

I verified that Chrome is sending "Accept-Encoding: gzip, deflate, br" in the request.

接受编码

但是,服务器未对响应进行gzip编码:

However, the server is not gzip-encoding the response:

内容编码

我在做什么错了?

我找到了这个讨论,并尝试了所有内容,但是没有帮助.

I found this discussion, and tried everything from there, but it didn't help.

推荐答案

我遇到了类似的问题(在Mac上为asp.net核心)

I faced a similar problem (asp.net core on Mac)

解决方案是确保在静态文件投放之前将响应压缩称为"

Solution was to make sure response compression was called BEFORE static file serving.

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseCookiePolicy();

        // ordering matters here
        app.UseResponseCompression();
        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

正确订购后,我可以在Chrome中以br(Brotli压缩)编码对其进行查看:

After ordering it correctly I could see it in chrome under encoding br (Brotli Compression):

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

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