ASP.NET MVC 2 在部分视图中禁用浏览器后退按钮的缓存

发布于 2024-09-02 07:34:22 字数 458 浏览 6 评论 0原文

我在我的主页上使用 Html.RenderAction(c => c.Show()); 来显示所有页面的购物车。问题是当我将商品添加到购物车然后点击浏览器后退按钮时。它显示旧购物车(来自缓存),直到我点击刷新按钮或导航到另一个页面。

我已经尝试过这个并且它工作完美但它禁用整个页面和我网站中所有页面的全局缓存(因为在母版页上使用了此操作方法)。出于性能原因,我需要为其他几个部分视图(操作方法)启用缓存。

我不想在页面加载时使用带有 AJAX 的客户端脚本来刷新购物车(和登录视图) - 但这是我现在能想到的唯一解决方案。

有谁知道更好吗?

I am using Html.RenderAction<CartController>(c => c.Show()); on my master Page to display the cart for all pages. The problem is when I add an item to the cart and then hit the browser back button. It shows the old cart (from Cache) until I hit the refresh button or navigate to another page.

I've tried this and it works perfectly but it disables the Cache globally for the whole page an for all pages in my site (since this Action method is used on the master page). I need to enable cache for several other partial views (action methods) for performance reasons.

I wouldn't like to use client side script with AJAX to refresh the cart (and login view) on page load - but that's the only solution I can think of right now.

Does anyone know better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨凡馨 2024-09-09 07:34:22

除非您使用 iframe 或 ajax,否则无法仅禁用页面的一部分的浏览器缓存。浏览器只是从缓存中提取数据,您可以禁用页面缓存,也可以不禁用。

Unless you use an iframe or ajax, there is no way to disable the browser cache for only a part of the page. The browser just pulls the data back from it's cache, and either you disable the pages cache or not.

烟燃烟灭 2024-09-09 07:34:22

ASP.NET MVC 中的甜甜圈洞缓存

如果您想缓存除购物车之外的所有页面。
您可以实现一个包含购物车的视图控件。并从此视图控件中删除缓存策略。

<%@ Control Language="C#" Inherits="ViewUserControl<IEnumerable<Joke>>" %>
<%@ OutputCache Duration="100" VaryByParam="none" %>

<ul>
<% foreach(var joke in Model) { %>
    <li><%= Html.Encode(joke.Title) %></li>
<% } %>
</ul>

Haacked 在此处对此进行了更详细的解释。

希望对您有帮助。

Donut Hole Caching in ASP.NET MVC

If you want to cache all your page except the cart.
You could implement a view control that contains the cart. and remove the cache policy from this view control.

<%@ Control Language="C#" Inherits="ViewUserControl<IEnumerable<Joke>>" %>
<%@ OutputCache Duration="100" VaryByParam="none" %>

<ul>
<% foreach(var joke in Model) { %>
    <li><%= Html.Encode(joke.Title) %></li>
<% } %>
</ul>

Haacked explains it in further detail here.

Hope it helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文