Mastering response pressure programs at ASP.NET CORE

This guide provides a comprehensive wandering of the response pressure at ASP.NET CORE. It begins with basic concepts such as the network reinforcement of the network and the mediator in the ASP.NET CORE. Then explore what the response pressure is, whenever intermediate programs use response pressure, pressure providers, and intermediate programs, and eventually the intermediary results of the response pressure. By the end of this guide, you will have a comprehensive understanding of the topic.
Let’s dive into!
Network domain display
The display of the frequency range indicates the maximum amount of data that can be sent via a network connection, whether wireless or wireless, within a specific time frame. It is measured in parts (B), kilos (kilo by), MB (MB), or Gigabits (GB) per second. Since the frequency range is a limited resource, it is important to manage it effectively. For example, the width of the roster range of the network 1 megabytes per second means that one million bits can be transported every second. If more data is sent than the network can treat, this may lead to crowding, which leads to low performance. Therefore, smaller response sizes can be transferred more quickly, which in turn enhances the performance of the application in general.
The intermediate in Asp.net Core
The intermediate component is added to the application pipeline to manage incoming requests and the responses issued. The intermediary has been provided for response compression since ASP.NET CORE 1.1.
Response pressure
The customer includes “”Accept coding“He headed in his request, while the server responds to”Contest content“The head that indicates how the coding response. It is important to note that the application of additional pressure on compressed files already, such as CSS and HTML, may lead to larger file sizes.
When does intermediate programs use response pressure:
Service like IIS and nginx It comes with compact response compression units, while http.sys and Caserel The servers do not have this feature. When an application is hosted on a server that lacks the compact response pressure, the pressure pressure programs are used on the response pressure. However, it is important to note that the performance of intermediary programs is the response to the response that tends to be less efficient compared to server units.
Click with https
the Enableforhttps The option controls pressure on safe communications. Usual crime and Break Risk attack when Enableforhttps He was appointed right.
Pressure off
There are two types of pressure provisions available: Proto and GZIP. The default pressure provider is determined by the customer’s data format. For example, if the customer supports the compressed Brotli data format, the default pressure will be adjusted on Brotli. If the customer does not support Brotli but supports GZIP, GZIP will be used as a default pressure method. The pressure level can also be formed. Protley is shortening Presse provider. For the BROTLI pressure provider, you can adjust the pressure level using BrotlicrasSIONPREPROVAVEROAFTIONS. Likewise, for the GZIP pressure provider, use GZIPCOCOCRESSIONPROVAROVEROAFTIONS. There are four levels of pressure available, and you can choose what is appropriate CompressionLENNUM The value is based on your specific needs, whether you give priority to speed or size. Available levels are:
- better
- faster
- NOCER pressure
- Smaller
Preparation of the mediator
The code below is the PROGRAM.CS file in .NET 8 Minimal Web API project. I added comments in the code for clarity.
//.net 8 minimal web api //Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// configure the services for response compression
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
// when you want to configure a custom compression provider
//options.Providers.Add<CustomCompressionProvider>(); });
//when brotli compression provider is used then the priority is the fastest.
builder.Services.Configure<BrotliCompressionProviderOptions>(options => { options.Level = CompressionLevel.Fastest; });
//when gzip compression provider is used then the priority is the size.
builder.Services.Configure<GzipCompressionProviderOptions>(options => { options.Level = CompressionLevel.SmallestSize; });
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger(); app.UseSwaggerUI();
}
// add the response compression middleware to the request pipeline
// must be called before any middleware that compresses responses
app.UseResponseCompression();
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
app.MapGet("/weatherforecast", () => {
var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast
( DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55), summaries[Random.Shared.Next(summaries.Length)] )) .ToArray(); return forecast;
}) .WithName("GetWeatherForecast");
app.Run();
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
// CustomCompressionProvider.cs
public class CustomCompressionProvider : ICompressionProvider
{
public Stream CreateStream(Stream data) { return data; }
}
Results and conclusion
Below is a schedule that compares the compressed volume and time based on pressure suppliers (Brotli, GZIP) and three pressure levels (faster, smaller size, perfect).
The intermediate pressure plays a decisive role in reducing the requirements of the frequency range and improving the application’s response. However, it can sometimes have negative effects if the chosen pressure provider or pressure level does not meet your specific needs. Therefore, it is important to exercise caution when using intermediate pressure programs to ensure their compatibility with the application requirements.
No Compression
436 B
6 ms
------------------------------------------------------------------------------------------------------------------------------------------------------
Brotli
Fastest
287 B
67 ms
SmallestSize
241 B
43 ms
Optimal
246 B
45 ms
------------------------------------------------------------------------------------------------------------------------------------------------------
Gzip
Fastest
289 B
50 ms
SmallestSize
289 B
50 ms
Optimal
290 B
33 ms
Screen shots
Cheer request:
Response head:
NOCERST pressure response details: