How To Enable Gzip Compression In Asp Net Core Using Dot Net CLI With Example

Krunal Shah

Introduction

Gzip is a compression format that can be applied to any stream of bits. In terms of how it’s used on the web, it’s easiest thought of a way that makes your files “smaller”. When applying gzip compression to text files, you can see anywhere from 70-90% savings, on images or other files that are usually already “compressed”, savings are much smaller or in some cases nothing. Other than eliminating unnecessary resource

Enabling GZip dynamically at the code level

Using dynamic .NET CORE API libraries you can Gzip any text based files on request. This is great for content that may change (Think dynamic html, text, not JS, CSS) . The reason it’s not great for static content such as CSS or JS, is that those files will not change between requests, or really even between deployments. There is no advantage to dynamically compressing these each time they are requested.

The code you need to make this work is contained in a Nuget package. So, you will need to run the following from your console.

Install-Package Microsoft.AspNetCore.ResponseCompression

ConfigureServices method in your startup.cs

public void ConfigureServices(IServiceCollection services)
{
 services.AddMvc();
 services.AddResponseCompression();
}

You can also add other options

services.AddResponseCompression(options =>
{
 options.EnableForHttps = true;
 options.MimeTypes = new[] {"text/plain"};
});

GZip of certain mimetypes

"text/plain" 

"text/css" 

"application/javascript" 

"text/html" 

"application/xml" 

"text/xml" 

"application/json" 

"text/json"

configure method of startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
 app.UseResponseCompression();
 app.UseMvc();
}

Some of Basic Question on GZip

  • What is gzip?

  • how gzip compression is works?

  • What is the GZIP Compression advantages?

  • What is GZIP Compression in ASP.NET Core?

  • How to make API call faster throw Gzip?

  • How to speed up your Website throw gzip?

  • How to use gzip compression in asp net core?

  • How to use gzip in asp net?

  • Does asp net core support response compression?

  • How to implement gzip compression in C#?

  • Can JSON be Gzipped?

  • Is gzip worth compression?


More Stories

SOLID principles in C# with examples

SOLID principles are a set of design principles that can be used to improve the quality of Csharp code. By following these principles, developers can create code that is more flexible, readable, testable, maintainable, and reusable.

Krunal Shah

Exploring the Benefits of Using Partial Methods in C#: A Beginners Guide

Learn about Partial Methods in C#: a powerful feature for customizing auto-generated code, split method declaration and implementation within partial class or struct, with many options. #CSharp #PartialMethods

Krunal Shah

CSRF Tokens In AngularJS jQuery With ASP.NET Core: A Comprehensive Guide

Learn how to implement CSRF tokens in AngularJS/jQuery with ASP.NET Core to enhance your web application security. This article provides a step-by-step guide to help you secure your web application against cross-site request forgery attacks.

Krunal Shah