在 MVC 3 中禁用局部视图的缓存
我有一个问题,部分视图在不应该缓存的情况下被缓存。该部分视图用于在页面上显示登录/注销。它使用下面的简单代码来确定要显示哪个链接。
@if(Request.IsAuthenticated) {
<a href="@Url.Action("LogOff", "Account", new { area = "" })">Log Off</a>
}
else {
<a href="@Url.Action("LogOn", "Account", new { area = "" })">Log On</a>
}
这个部分视图是从我的 MVC3 应用程序中的所有页面调用的,使用
@Html.Partial("_HeaderView")
在我的大多数控制器中,我定义了输出缓存,因此我可以利用缓存我的内容。
[OutputCache(Duration = 86400, VaryByParam = "*")]
现在我的问题是,当我不希望部分视图被缓存时,整个页面都被缓存。这会导致错误的行为,即使用户未登录等,有时也会显示注销等。有没有办法缓存所有内容,除了有问题的部分视图之外?
I have an issue with a partial View being cached when it shouldn't be. This partial View is used to display the Logon/Logoff on a page. It uses the simple code below to figure out which link to display
@if(Request.IsAuthenticated) {
<a href="@Url.Action("LogOff", "Account", new { area = "" })">Log Off</a>
}
else {
<a href="@Url.Action("LogOn", "Account", new { area = "" })">Log On</a>
}
This partial View is called from with all pages in my MVC3 application, using
@Html.Partial("_HeaderView")
In most of my controllers, I have the output cache defined, so I can take advantage of caching my content.
[OutputCache(Duration = 86400, VaryByParam = "*")]
Now my issue is that the entire page is being cached when I don't want the partial view to be. This is causing wrong behavior where in it sometimes displays LogOff even if the user is not logged in etc. Is there a way to cache all the content, except for the partial view in question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以通过使用以下内容装饰显示 _HeaderView 部分的控制器来禁用缓存:
You can disable caching by decorating the controller which displays your _HeaderView partial with the following:
您正在寻找的称为“甜甜圈缓存”。这是一篇很棒的文章,解释了它是什么以及如何使其工作 http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3
What you are looking for is called Donut Caching. Here's a great article explaining what it is and how to make it work http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3
我们应该在Web.config文件中设置缓存配置文件,而不是在页面中单独设置缓存值,以避免冗余代码。我们可以使用 OutputCache 属性的 CacheProfile 属性来引用配置文件。此缓存配置文件将应用于所有页面,除非页面/方法覆盖这些设置。
如果您想从返回部分视图 [_HeaderView] 的操作中禁用缓存,您可以通过装饰特定操作方法来覆盖配置缓存设置,如下所示:
希望这会对您有所帮助!
We should set cache profile in the Web.config file instead of setting cache values individually in pages to avoid redundant code. We can refer the profile by using the CacheProfile property of the OutputCache attribute. This cache profile will be applied to all pages unless the page/method overrides these settings.
And if you want to disable the caching from your action which returns partial view [_HeaderView], you can override the config cache settings by decorating that specific action method like shown below:
Hope this would help you!
这对我有用..
this working for me..
回到 MVC 后花了一点时间才解决这个问题。只需将缓存设置直接放在部分标题视图中即可。就像显示用户名时一样。不需要全局或服务器端代码。唯一的问题是一旦页面被缓存,登录后它不会立即刷新。但我们在初次浏览产品时会在需要时保持速度。在我们的例子中可以权衡一下。
Took a little while to figure this one out after getting back into MVC. Just put the Cache setting directly in the Partial Header View. As in when displaying the user name. No need for global or server-side code. Only problem is once a page is cached, it will not refresh right away after login. But we keep the speed when needed in the initial browsing for products. Ok trade off in our case.
快进到 2023 年,在 .NET Core 中,您可以执行类似的操作来关闭页面某些部分的缓存:
在此处阅读有关
-tag helper 的更多信息:https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/cache-tag-helper?view=aspnetcore-7.0Fast forwarding to 2023, in .NET Core you can do something like this to turn off cache for some parts of the page:
Read more about the
<cache>
-tag helper here: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/cache-tag-helper?view=aspnetcore-7.0