As in Net MVC, I want to save the response cache to the server (it's called outputcache), but there is no such feature in Net Core or Net 5. I couldn't find an alternative method. Thank you in advance.
Asked
Active
Viewed 409 times
2
-
Hi @Blackerback, you can refer to [this doc](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/response?view=aspnetcore-6.0) to learn Response caching in ASP.NET Core. – Rena Nov 30 '21 at 06:34
-
1Hello @Rena, I have reviewed this and many documents many times, but when I press the F5 key, all cache data is deleted, I need to use server side cache structure to prevent it from being deleted (OutputCache Location.Server etc...). – Blackerback Nov 30 '21 at 07:04
1 Answers
1
You can use WebEssentials.AspNetCore.OutputCaching nuget to achieve your requirement.
Follow the steps below:
1.Add the middleware:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddOutputCaching();
//other middleware...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseOutputCaching();
//other middleware...
}
}
2.Add OutputCache
attribute to the action:
[OutputCache(Duration = 600)]
public IActionResult Index()
{ ... }
A sample code for testing:
Controller:
[OutputCache(Duration = 600)]
public IActionResult Index()
{
return View(DateTime.Now);
}
View:
@model DateTime
Time of request: @Model.ToString()
Try requesting the page and notice that the date and time remains the same, even when reloading the page, until the cache has expired.
OutputCache contains not least Duration
option, but also other options, such as VaryByHeader
, VaryByParam
and so on...
More details you can refer to the github repo for WebEssentials.AspNetCore.OutputCaching

Rena
- 30,832
- 6
- 37
- 72
-
1
-
It works fine for me. I want to write a middleware about it, that it will cache or not according to the incoming route value. How can I use this? – Blackerback Nov 30 '21 at 09:05
-
1I suggest you can use MapWhen middleware like below:https://i.stack.imgur.com/OU6H8.png. The detailed document: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-6.0#branch-the-middleware-pipeline – Rena Nov 30 '21 at 09:25