开发者

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

开发者 https://www.devze.com 2022-12-15 07:10 出处:网络
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.

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 bac开发者_如何学编程k, 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号