I want to clear the response cache on the button click by programmatically
Loading
I want to clear the response cache on the button click by programmatically
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Alpesh ManiyaPosted Feb 21, 2024, 5:25 AM
Thanks, @Bhavesh Raval for your response. However, if we set the duration of 0 then there is no use of the response cache. My concern is it should be clear only if we call clear cache explicitly.
Bhavesh RavalPosted Feb 20, 2024, 2:15 PM
In .NET Core, you can programmatically clear the response cache by using the
ResponseCacheAttributeand setting itsNoStoreproperty totrue. This attribute can be applied to an action method or a controller to prevent caching of the response.Here's how you can use it:
using Microsoft.AspNetCore.Mvc; [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult MyAction() { // Your action code here }
In this example,
NoStoreis set totrue, indicating that the response should not be stored in the cache. Additionally,Durationis set to0, meaning that the response is immediately considered stale, andLocationis set toResponseCacheLocation.None, indicating that the cache headers should not be included in the response.By applying this attribute to your action method or controller, you ensure that the response is not cached. This can be useful when you want to prevent caching for specific actions that generate dynamic content or data that should not be cached.