我无法使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中不起作用
发布评论