To ensure better security and consistency, I made sure that my pages were not cached. Whenever I pressed the back button on my browser, it always contacted the server to retrieve the HTML content.
I accomplished this by implementing a custom action filter as shown below:
public class NoCache : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
}
I applied this global filter to all actions using the following code snippet:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
// Prevents any cached pages from being served to browsers, including Chrome
filters.Add(new NoCache());
}
While this solution solved the initial problem, it also prevented Images, CSS, and JavaScript files from being cached. How can I instruct the application to cache them?