Skip to main content

ASP.NET Core Gzip Response Compression

Compress your HTML and static files

There's a package available for providing compression middleware.

"Microsoft.AspNetCore.ResponseCompression" : "1.0.0"

Add the the services

public void ConfigureServices(IServiceCollection services)
{
    // Response Compression
    services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);
    services.AddResponseCompression(options =>
    {
        // Enable HTTPS if required
        options.EnableForHttps = true; 
        options.Providers.Add<GzipCompressionProvider>();

        // Add extra mime types to the defaults
        options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[]
        {
              "image/svg+xml",
              "application/font-woff2"
         });
    });

    services.AddMvc();
}

Configure the pipeline

public void Configure(IApplicationBuilder app)
{
    // Response Compression
    app.UseResponseCompression();

    // MVC and static files come after
    app.UseStaticFiles();
    app.UseMvc();
}

Hello, my name is Lee and I work as a full-stack web developer specialising in Microsoft ASP.NET technologies. I love using Umbraco and also MANAGED, my own application management software.

Contact me at lee.gunn@secretorange.co.uk

All skills

Contact

Get in touch to talk about your project or just ask me a question.

lee.gunn@secretorange.co.uk