I want to clear the response cache on the button click by programmatically
How can we clear the response cache programmatically in .net core
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Bhavesh RavalRank #1,245 · 1.4K reputationPosted Feb 20, 2024, 9:15 AM EST
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.
Alpesh ManiyaRank #832 · 2.7K reputationPosted Feb 21, 2024, 12:25 AM EST
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.