Response Caching in ASP.NET Core stores HTTP responses temporarily to reduce server load and improve app performance. It is useful for frequently requested data that doesn’t change often. ASP.NET Core provides middleware and attributes to control caching behavior.
Key Features:
- Reduces server processing and database calls
- Supports cache duration, location, and vary-by parameters
- Works with MVC, Razor Pages, and APIs
- Easy integration using attributes or middleware
- Improves performance and scalability
Example:
Enable response caching in Program.cs:
builder.Services.AddResponseCaching();
var app = builder.Build();
app.UseResponseCaching();
Use attribute on a controller action:
[ResponseCache(Duration = 60, Location = ResponseCacheLocation.Client)]
public IActionResult Index()
{
return Content($"Current Time: {DateTime.Now}");
}
Response Caching in ASP.NET Core enhances performance by serving cached content quickly while reducing server workload.
Citations: