esastincy
esastincy

Reputation: 1627

ASP.NET MVC 1 Page Caching Problem

On my site there is a page where a user can see a list of all the payments they need to make, select a file, and make that payment. The problem I am having is if the user makes a payment on a file and then types in the URL of the payment grid page a cached page appears still showing the file that has already been paid, allowing the user to submit a second payment. What I would like to do is always go through the controller so if the user types in this URL they would re-generate the model and this file would no longer appear. Is there a way to turn of caching for a certain page? Any ideas on how to work around this?

Upvotes: 2

Views: 320

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039080

You could create a custom action filter to set proper response headers in order to instruct the browser not to cache the results of the page:

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

and then decorate the controller action with this attribute:

[DisableCache]
public ActionResult PerformPayment()
{
   ...
}

Upvotes: 2

Related Questions