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.
All the modern Browsers support response compression. Using
GZIP Compression is the best approach to reduce the response size and provide better speed for transferring the files over the network.
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.
Step 2
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
-
- public void ConfigureServices(IServiceCollection services)
- {
-
- services.AddDbContext<ApplicationDbContext>(options =>
- options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
-
- services.AddIdentity<ApplicationUser, IdentityRole>()
- .AddEntityFrameworkStores<ApplicationDbContext>()
- .AddDefaultTokenProviders();
-
-
-
- services.AddResponseCompression();
-
- services.AddMvc();
-
-
- services.AddTransient<IEmailSender, AuthMessageSender>();
- services.AddTransient<ISmsSender, AuthMessageSender>();
- }
By default, ASP.NET Core uses GZIP compression. Now, let’s add this middleware to HTTP pipeline, so add the line of code given below in Configure method of Startup.cs.
Remember to add Compression middleware before other middlewares, which serves the files.
Code snippet
-
- 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();
-
-
-
- app.UseResponseCompression();
-
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- });
- }
So far we have installed and configured Response Compression in ASP.NET Core Application.
Creating dummy data for Test
Lets create a Test API to assess the data.
My TestAPI looks like, as shown below.
Code snippet
-
- [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;
- }
Application execution
Lets run the Application and navigate to API.
In my case, it is http://localhost:51381/TestAPI/Get
Case I
When Response Compression is disabled.
(Comment below lines in Startup.cs)
- services.AddResponseCompression(); (ConfigureServices method)
- app.UseResponseCompression(); (Configure Method)
Case II
When Response Compression is enabled.
(UnComment below lines in Startup.cs)
- services.AddResponseCompression(); (ConfigureServices method)
- app.UseResponseCompression(); (Configure Method)
Comparision
| 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 |
Summary
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.
You may also like