简体   繁体   English

如何防止ASP.NET MVC中的某些视图被缓存?

[英]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.

相关问题 是否有可以与 ASP.NET MVC 3 一起使用的属性来防止 model 字段自动包含在我的视图中? - Is there an attribute that I can use with ASP.NET MVC 3 to prevent model fields from being automatically included in my views? 如何将某些视图和文件夹排除在已部署的ASP.NET MVC NuGet包中? - How can I exclude some views and folders from being included into the deployed ASP.NET MVC NuGet package? 如何让我的控制器从他在 ASP.NET MVC 中的一个视图中捕获表单提交的数据 - How can I make my controller catch the data submitted by a form from one of his views in ASP.NET MVC 我如何计算ASP.NET MVC中的图像视图 - How can i count image views in ASP.NET MVC 如何在nUnit中对我的asp.net mvc应用程序的视图和授权属性进行单元测试? - How can I unit test views and authorization attributes of my asp.net mvc application in nUnit? 如何将数据库逻辑从我的 Asp.Net MVC 应用程序转移到 ASP.Net Core MVC? - How can I transfer the database logic from my Asp.Net MVC Apllication to ASP.Net Core MVC? 如何防止某些在ASP.Net MVC中具有某些特殊字符的URL执行 - How to prevent some url from execute which has some special charcters in ASP.Net MVC ASP.NET MVC:控制器上的增量计数器,正在缓存 - ASP.NET MVC: Increment counter on controller, being cached 如何将对象从我的视图传递到 ASP.NET MVC 中的控制器? - How can I pass an object from my view to my controller in ASP.NET MVC? 如何防止ASP.NET MVC控制器实例化和执行 - How to prevent an asp.net mvc controller from being instantiated and executed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM