Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GZIP in .net core not working

I'm attempting to add Gzip middleware to my ASP.net core app.

I have added the following package :

"Microsoft.AspNetCore.ResponseCompression": "1.0.0"

In my startup.cs for the Configure Services method I have the following :

public void ConfigureServices(IServiceCollection services) {     services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);     services.AddResponseCompression(options =>     {         options.Providers.Add<GzipCompressionProvider>();     });      services.AddMvc(); } 

In my Configure method I have the following :

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {     loggerFactory.AddConsole(Configuration.GetSection("Logging"));     loggerFactory.AddDebug();      app.UseResponseCompression();     app.UseMvc(); } 

However when I try and load a page, it doesn't come through as Gzip compressed. I have used both a string response and outputting a view. The response headers in chrome look like :

enter image description here

I am on a windows machine developing in visual studio. When running the app I have tried just running from Visual Studio (Via F5), and also using the "dotnet run" command from command line. Neither output GZip compression.

like image 225
MindingData Avatar asked Jan 19 '17 01:01

MindingData


People also ask

Does ASP NET core support response compression?

Add a Home Controller with Index action and write some data to the View. Notice the size and time of response before and after compression. When you're unable to use the compression features of web servers (IIS, Apache, Nginx), ASP.NET Core provides an alternate option, Response Compression middleware.

Is Brotli better than gzip?

Since Brotli was designed to compress streams on the fly, it is faster at both compressing content on the server and decompressing it in the browser than in gzip. In some cases the overall front-end decompression is up to 64% faster than gzip.

How to compress web api response?

To use compression, include the HTTP header Accept-Encoding: gzip or Accept-Encoding: deflate in a request. The REST API compresses the response if the client properly specifies this header. The response includes the header Content-Encoding: gzip or Accept-Encoding: deflate .


1 Answers

To Enable GZIP in .net core 2.*
1. Install Microsoft.AspNetCore.ResponseCompression with Install-Package Microsoft.AspNetCore.ResponseCompression command or nuget package manager.
2. Add the following code into Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {    app.UseResponseCompression();   app.UseMvc();  }  public void ConfigureServices(IServiceCollection services) {    // Configure Compression level   services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);    // Add Response compression services   services.AddResponseCompression(options =>   {       options.Providers.Add<GzipCompressionProvider>();       options.EnableForHttps = true;   });  } 
like image 181
Mohammad Dayyan Avatar answered Sep 27 '22 21:09

Mohammad Dayyan