ASP.NET Core Web API Interview Question #135

What is output caching in ASP.NET Core?

Error Handling, Logging, Caching, Rate Limiting, CORS & Reliability Senior Advanced

Quick Interview Answer

Output caching stores endpoint responses on the server and can reuse them according to configured policies. It provides application-controlled caching behavior and can reduce repeated work for suitable endpoints.

Detailed Explanation

Output caching stores endpoint responses on the server and can reuse them according to configured policies. It provides application-controlled caching behavior and can reduce repeated work for suitable endpoints.

Cache policies should define expiration, variation, and invalidation deliberately. Sensitive or per-user responses should not be shared through a cache unless the policy correctly partitions them.

Code Example

builder.Services.AddOutputCache();

app.UseOutputCache();

app.MapControllers();

[OutputCache(Duration = 60)]
[HttpGet("featured")]
public IActionResult Featured() => Ok();