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 :
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.
By placing the
UseResponseCompression
twice under each other I managed to make it work. I have no idea how this made it work. I am still looking for an accepted solution.I managed to enable the Response Compression Middleware when using IIS Express by removing
in .vs\config\applicationhost.config
To Enable GZIP in .net core 2.*
1. Install
Microsoft.AspNetCore.ResponseCompression
withInstall-Package Microsoft.AspNetCore.ResponseCompression
command or nuget package manager.2. Add the following code into
Startup.cs
Got this issue resolved by adding response compression option property "EnableForHttps" as shown below: