C# 在 MVC 3 中禁用部分视图的缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8786942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:25:37  来源:igfitidea点击:

Disable caching on a partial view in MVC 3

c#asp.net-mvc-3cachingpartial-viewsoutputcache

提问by SimpleUser

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

这个局部视图是从我的 MVC3 应用程序中的所有页面调用的,使用

@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?

现在我的问题是当我不希望部分视图被缓存时整个页面都被缓存。这会导致错误的行为,即使用户未登录等,有时也会显示 LogOff。有没有办法缓存所有内容,有问题的部分视图除外?

采纳答案by torm

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

您正在寻找的是所谓的甜甜圈缓存。这是一篇很棒的文章,解释了它是什么以及如何使其工作http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3

回答by Nick

You can disable caching by decorating the controller which displays your _HeaderView partial with the following:

您可以通过使用以下内容装饰显示 ​​_HeaderView 部分的控制器来禁用缓存:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult HeaderView()
{
    return PartialView("_HeaderView");
}

回答by Swati Gupta

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.

我们应该在 Web.config 文件中设置缓存配置文件,而不是在页面中单独设置缓存值以避免冗余代码。我们可以通过使用?OutputCache? 属性的?CacheProfile? 属性来引用配置文件。除非页面/方法覆盖这些设置,否则此缓存配置文件将应用于所有页面。

<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="CacheProfile" duration="60" varyByParam="*" />
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>

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:

如果您想从返回部分视图 [_HeaderView] 的操作中禁用缓存,您可以通过装饰该特定操作方法来覆盖配置缓存设置,如下所示:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult RenderPartialView()
{
    return PartialView("_HeaderView");
}

Hope this would help you!

希望这会帮助你!

回答by Miroslav

this working for me..

这对我有用..

 public ActionResult LogOff()
 {
     AuthenticationManager.SignOut();  
     var url = Url.Action("Index", "Web"); 
     HttpResponse.RemoveOutputCacheItem(url);           
     return RedirectToAction("Index", "Web");
 }

回答by Jeff

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.

回到 MVC 后花了一点时间才弄明白这个问题。只需将缓存设置直接放在部分标题视图中即可。就像在显示用户名时一样。不需要全局或服务器端代码。唯一的问题是页面缓存后,登录后不会立即刷新。但是我们在产品的初始浏览中会在需要时保持速度。好的,在我们的情况下进行权衡。

    @if ( Request.IsAuthenticated)
    {
            @* when we are authenticated, don't cache any more! *@
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.Cache.SetNoStore();
    HttpContext.Current.Response.Cache.SetNoServerCaching();
            @*@Html.Raw(DateTime.Now.ToString())*@
    @Html.ActionLink("Welcome " + ( String.IsNullOrEmpty(((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirstValue("UserName")) ? User.Identity.Name : ((System.Security.Claims.ClaimsIdentity)User.Identity).FindFirstValue("UserName")), "Index", "Manage", routeValues: new { Area = "Store" }, htmlAttributes: new { title = "Manage"})
    }
    else
    {
    }