ASP.NET Core 1.1 has an inbuilt Response Compression Middleware for compression, which by default uses GZIP compression. In simple words, GZIP is a technique, which is used for the file compression and decompression for faster network transfer.
Enable GZIP compression in ASP.NET Core
Let's first install GZIP in our ASP.NET MVC Project, using NUGET Package Manager.
Step 1
Right click on ASP.NET Core and select Manage Nuget Package.

Browse Microsoft.AspNetCore.ResponseCompression.
Once ResponseCompression is installed, we need to configure it.
Let's open Startup.cs file and add the code given below in ConfigurationService method.

Code snippet
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- // Add framework services.
- services.AddDbContext<ApplicationDbContext>(options =>
- options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
- services.AddIdentity<ApplicationUser, IdentityRole>()
- .AddEntityFrameworkStores<ApplicationDbContext>()
- .AddDefaultTokenProviders();
- //Add ResponseCompression as Service
- services.AddResponseCompression();
- services.AddMvc();
- // Add application services.
- services.AddTransient<IEmailSender, AuthMessageSender>();
- services.AddTransient<ISmsSender, AuthMessageSender>();
- }
Remember to add Compression middleware before other middlewares, which serves the files.
Code snippet
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
- loggerFactory.AddConsole(Configuration.GetSection("Logging"));
- loggerFactory.AddDebug();
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- app.UseDatabaseErrorPage();
- app.UseBrowserLink();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
- }
- app.UseStaticFiles();
- app.UseIdentity();
- // Add external authentication middleware below.
- //Compression middleware before other middlewares which serves the files.
- app.UseResponseCompression();
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- });
- }
My TestAPI looks like, as shown below.
- //[Route("api/TestAPI/Get")]
- [HttpGet]
- public IEnumerable<string> Get()
- {
- List<string> data = new List<string>();
- for (int i = 1; i <= 100; i++)
- {
- data.Add("ID :" + i.ToString());
- data.Add("Name :" + i.ToString());
- data.Add("Address :" + i.ToString());
- data.Add("Email :" + i.ToString());
- data.Add("Telephone :" + i.ToString());
- }
- return data;
- }
Lets run the Application and navigate to API.
(Comment below lines in Startup.cs)
- services.AddResponseCompression(); (ConfigureServices method)
- app.UseResponseCompression(); (Configure Method)

(UnComment below lines in Startup.cs)
- services.AddResponseCompression(); (ConfigureServices method)
- app.UseResponseCompression(); (Configure Method)

| Response Compression Disabled | Response Compression Enabled | |
| Transferred data | 6.4 KB | 1.4 KB |
| Finish time | 32 ms | 14 ms |
| Load time | 135ms | 81 ms |
Thus we learned,
- How to install Response Compression library in ASP.NET Core.
- How to enable Response Compression as middleware.
- How to transfer the data over the network by compressing.
- Decrease Transfer and Load time of the data.
- How to configure Email Services in ASP.NET Core Applications.
- How to export data to Excel from Web API in ASP.NET Core using Kendo UI
- Sorting and Grouping in Kendo Grid using Web API in ASP.NET Core Application
- Call Web API Using JQuery AJAX In ASP.NET Core Web Applications
- How to create Image Thumbnail in ASP.NET Web Application

Harish PaudelPosted Apr 26, 2020, 8:34 AM
10x friend new thing i learn today from your articles, thanks once again for your efforts.
Parth AkbariPosted Oct 17, 2018, 12:33 AM
I want to compress file and sent it to browser. is it possible? if yes, then browser download that file so, it will decompress it or not?
Ryan BattistonePosted Feb 13, 2018, 12:20 PM
This is actually a very vulnerable method. Do not use this, unless your website is static, and does not use any sort of password or login authentication, any kind of API tokens, etc. https://security.stackexchange.com/questions/19911/crime-how-to-beat-the-beast-successor/19914