简体   繁体   中英

How can I prevent some of my views in ASP.NET MVC from being cached?

I have a view that shows a list of items and when you click on one of them it will take you to the items page. I also store a "Viewed" flag in the database for that item.

Now, when the user clicks back, the item should be styled differently because of the change in the "Viewed" flag. However, every time I click back, it is as it was before I clicked into the item, and I have to click refresh to see the actual state of the page now.

How can I prevent this page from being cached so when a user clicks back they will see the latest version of this site, complete with the new styling?

Mark the controller action that generates the list with the OutputCacheAttribute and set the cache location to none to prevent that page from being cached on the client. This should cause the client to request the page again. If the user is using the back button, however, I think that the page is served up by the browser without reloading regardless of the caching. At least in FF I don't see it requesting the page again using Firebug.

[OutputCache( Location = OutputCacheLocation.None )]

Call this in your controller action:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

This will prevent the browser from caching the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM