In response to the cacheRazor pages are not supported in ASP.NET Core 2.0. This feature will be supportedASP.NET Core version 2.1.

In older versions of MVC, there was a feature that could cache views (OutputCache). Requests with the same parameters could be read directly from MVC’s cache for N periods of time without following the view’s logic.

[OutputCache(Duration =20)]// Set expiration time to 20 seconds public ActionResult ExampleCacheAction() {var Time = datetime.now. ToString("yyyy "); ViewBag.time= time; return View(); }Copy the code

In Asp.Net core 2.1, the official documentation states that response caching reduces the number of requests made to the web server by clients or proxies. Response caching also reduces the amount of work the Web server executes to generate the response. Response caching consists of headers that specify how you want the client, proxy, and middleware caching the response to be controlled.

In Asp.Net Core 2.1, the ResponseCache parameter is replaced by OutputCache, and the ResponseCache parameter must be set to “Duration” in seconds, at least one second

[ResponseCache(Duration = 5)] public IActionResult About() {viewbag.time = datetime.now.ToString("yyyy ") {viewbag.time = datetime.now HH mm mm ss second "); return View(); }Copy the code

The browser then requests the view

 

Max-age =5 appears in cache-control in the browser response header, which is interpreted by the Http protocol

The client will not accept a response whose retention time is longer than the specified number of seconds. Example:max-age=60(60 seconds),max-age=2592000(1 month)

If caching is disabled in the browser, the ResponseCache has no effect


 

Than filtering

[ResponseCache(VaryByHeader = "User-Agent", Duration = 5)] public IActionResult About() {viewbag.time = datetime.now.toString ("yyyy "); return View(); }Copy the code

 

About than in the Http response header is used to tell or CDN cache server, I am still the same browser requests, you gave me the cache line, if you change a browser to request, less than the value of the affirmation is empty, the cache server will think you are a new request, will go to read the latest data to the browser

Reference: www.w3.org/Protocols/r…

Disable caching (NoStore and location.None)

In Http: no-store, neither request nor response information should be stored on the other party’s disk system;

[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult About() {viewbag.time = datetime.now.toString (" YYYY "); return View(); }Copy the code
ResponseCacheLocation. None is in a no - cache-control Settings Cache attribute, the browser does not Cache the current URLCopy the code
In a normal project, there are certainly many controllers, but not all of them have the same cache policy. In this case, we need a cache configuration to be flexible about this. We can inject our cache policy in optionCopy the code
services.AddMvc(option=> {
                option.CacheProfiles.Add("test1", new CacheProfile()
                {
                    Duration = 5
                });
                option.CacheProfiles.Add("test2", new CacheProfile()
                {
                    Location = ResponseCacheLocation.None,
                    NoStore = true
                });
            });
Copy the code

Then we use the name of the configuration policy

[ResponseCache(CacheProfileName = "test1")] public IActionResult About() { ViewBag.time = Datetime.now. ToString("yyyy "); datetime.now. ToString(" YYYY "); return View(); }Copy the code

This way we can configure the same after the feature as before, and the code looks much cleaner

 

Summary: My personal understanding of response caching is this: MVC returns an HTTP response header to allow the browser to perform a refresh operation without asking the server to read directly from the cache…


 

If it helps, you’re welcome to buy me a bottle of water