Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load page freshly and not from cache

Is there way I can force page not to load from cache? Everytime page is loaded it is loaded from server. I'm using asp.net MVC 3.

like image 550
pramodtech Avatar asked Dec 07 '25 12:12

pramodtech


1 Answers

You could use a custom no cache action filter:

public class NoCacheAttribute : ActionFilterAttribute
{  
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var cache = filterContext.HttpContext.Response.Cache;
        cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        cache.SetValidUntilExpires(false);
        cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        cache.SetCacheability(HttpCacheability.NoCache);
        cache.SetNoStore();
        base.OnResultExecuting(filterContext);
    }
}

Then decorate any controller/action with this attribute that you don't want to be cached by client browsers.

like image 106
Darin Dimitrov Avatar answered Dec 10 '25 12:12

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!