Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to compression and cache of ASP.NET Core middleware

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "ASP.NET Core middleware compression and cache introduction", the explanation content in the article is simple and clear, easy to learn and understand, please follow the idea of Xiaobian slowly in-depth, together to study and learn "ASP.NET Core middleware compression and cache introduction" bar!

preface

Today, I will introduce you to two middleware that are used in ASP.NET Core daily development. They are all from Microsoft's ASP.NET team. They are Microsoft.AspNetCore.ResponseCompression and Microsoft.AspNetCore.ResponseCaching. Let's take a look at the functions and how to use them.

Getting Started

Microsoft.AspNetCore.ResponseCompression

Microsoft.AspNetCore.ResponseCompression This middleware is a new addition to the. NET Core 1.1 version. You should know from the name that it is mainly responsible for compressing the output content.

Gzip compression is a performance optimization technique that we often use in WEB, it can use compression algorithm (GZip) to compress the volume of page output content, that in the past, we can use IIS to do this work, but now our program is separated from IIS, we must have a middleware to help us do this thing, it is the middleware we want to introduce.

Add Microsoft.AspNetCore.ResponseCompression package

You can use Visual Studio to open NuGet package manager console and enter a command to install

Install-Package Microsoft.AspNetCore.ResponseCompression

It can also be installed using the NuGet package manager UI interface.

After adding, you can see the packages you added in project.json. Note that the current version is 1.0.0.

Update Startup.cs file

Modify Startup and add the following code to ConfigureServices and Configure methods:

public class Startup{ ... public void ConfigureServices(IServiceCollection services) { services.AddResponseCompression(); ... } public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { app.UseResponseCompression(); ... }}

Now you can test whether the input Http Response is compressed.

Former:

After:

By comparing the front and back, we can see that there is an additional Content-Encoding:gzip header information in Response Headers, indicating that our middleware is in effect.

Microsoft.AspNetCore.ResponseCaching

Microsoft.AspNetCore.ResponseCaching This middleware is also a new addition to the. NET Core 1.1 version. It should also be known from the name. It is mainly responsible for caching the output content. In the past we could have done the same in IIS, but the granularity might not have been as fine.

I previously wrote an article about ASP.NET Core caching, which introduced the Response cache in ASP.NET Core MVC, which is implemented through a ResponseCacheAttribute to set cache header information:

[ResponseCache(VaryByHeader ="Accept-Encoding", Location = ResponseCacheLocation.Any, Duration = 10)]public IActionResult About(){}

In addition to ResponseCacheAttribute provided by MVC, there is another way to set cache header information, as follows:

public IActionResult About(){ Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue() { Public = true, MaxAge = TimeSpan.FromSeconds(10) }; Response.Headers[HeaderNames.Vary] = new string[] { "Accept-Encoding" };}

Both ways, the final effect is consistent.

With this header information, we can do something with the middleware on the server side. Therefore, the middleware will read the header information at the appropriate time and cache it in the local cache. When another request comes in, it will skip the action directly and read the cached information to return.

Let's see how we can add it to our project. It's easy.

Add Microsoft.AspNetCore.ResponseCaching package

You can use Visual Studio to open NuGet package manager console and enter a command to install

Install-Package Microsoft.AspNetCore.ResponseCaching

Update Startup.cs file

Modify Startup and add the following code to ConfigureServices and Configure methods:

public class Startup{ ... public void ConfigureServices(IServiceCollection services) { services.AddResponseCaching(); ... } public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { app.UseResponseCaching(); ... }}

Enter header information as follows:

Detailed examples can be found here.

Note: In Chrome, Control-Cache: max-age in the Http Response Header may sometimes not work when you press F5 or right-click to refresh the page, because Chrome has intelligent algorithms to guess whether your current behavior is really intended to refresh or cache. So you can try putting your address into an HTML Link or open a new tab to type in the address to try. Edge and IE browser behavior is expected.

Thank you for reading, the above is the "ASP.NET Core middleware compression and cache introduction" content, after learning this article, I believe we have a deeper understanding of ASP.NET Core middleware compression and cache introduction this issue, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report