Gzip压缩中间件在ASP.NET Core 2.0中不起作用

编程入门 行业动态 更新时间:2024-10-26 18:26:59
本文介绍了Gzip压缩中间件在ASP.NET Core 2.0中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am unable to get Gzip compression middleware to work in asp 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核心)

I faced a similar problem (asp 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中不起作用

本文发布于:2023-11-16 11:54:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1604052.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   中间件   ASP   Gzip   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!