【问题标题】:MVC 3 Prevent partial View to cacheMVC 3 防止部分视图缓存
【发布时间】:2012-01-05 19:01:22
【问题描述】:

我正在使用带有剃须刀的 MVC 3。我有一些从主视图调用的部分视图。在部分视图中,我想显示一些 DB 值。当我更改数据库值时,它会显示缓存中的旧值。那么我如何才能在部分视图上停止缓存呢?

 @Html.Partial("_myPartialView", Model)

谢谢

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    您的代码可能正在缓存所有内容(默认情况下可能是浏览器缓存),因此您确实需要正在处理的甜甜圈洞缓存。 查看: http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3

    【讨论】:

      【解决方案2】:
       $(function () {
       $.ajaxSetup ({
            // Disable caching of AJAX responses
            cache: false }); 
      )};
      

      应该这样做!

      【讨论】:

        【解决方案3】:

        上面的代码默认不使用缓存。如果你使用

        @Html.Partial("_myPartialView", Model)
        

        它使用来自 Model 对象的数据呈现 _myPartialView,而没有任何缓存。您的问题必须由其他原因引起。也许构造模型对象的数据检索代码使用了一些数据层缓存?发布更多代码会有所帮助。

        【讨论】:

          【解决方案4】:

          根据设置,在局部视图上禁用缓存的一种可能方法是将其分解为单独的客户端调用,即进入 jQuery/Ajax。

          否则这个主题的变化怎么样。

          回到 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
          {
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-20
            • 2011-11-16
            • 1970-01-01
            • 1970-01-01
            • 2014-01-23
            相关资源
            最近更新 更多