[英]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. 使用OutputCacheAttribute标记生成列表的控制器操作,并将缓存位置设置为none,以防止该页面缓存在客户端上。 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. 至少在FF中,我看不到它使用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. 这将防止浏览器缓存页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.