欧博娱乐out of cached responses in .Net 8 ASP.NET Web
In implementing the newest version of .Net's response caching middleware, we need to make a policy that allows callers to bypass cached responses if they send a specific header key.
For the sake of simplicity, key values of "bypass-cache" = "true" are sufficient for an example.
// Program.cs // ...contents omitted from brevity. builder.Services.AddOutputCache(o => { // Allow callers to bypass the cache with a specific header value. o.AddBasePolicy(o => o.With(r => r.HttpContext.Request.Headers["bypass-cache"] == "true") .NoCache() ); }); // ...contents omitted from brevity. // I have experimented with moving this up and down to see if the order of middleware impacted it. app.UseOutputCache(); // ...contents omitted from brevity.The controllers are decorated with the OutputCacheAttribute
[HttpGet] [OutputCache] public async Task<IActionResult> ReadById(int id) { // Even when the value of this `check` is true, the policy is ignored 😠! var check = HttpContext.Request.Headers["bypass-cache"] == "true"; var request = new ReadCrudByIdRequest(id); var result = await _mediator.Send(request); return Ok(result); }Post man is being used to manually this endpoint. As mentioned in the comment, I can see in the controller when the value is true or false, but it does not have any impact on the Policy. I have also attempted creating an explicitly named policy and labeling the endpoint with [OutputCache(PolicyName = "bypass-cache")] but the result is the same.
Any help is much appreciated, thanks!
2025-07-17 08:20 点击量:2