【问题标题】:Clear cache in mvc3.清除 mvc3 中的缓存。
【发布时间】:2014-03-14 07:11:07
【问题描述】:

我正在使用 Sql server 上传图片,上传的图片立即反映在下面的网格中。我的问题是当我更改或编辑图片时,图片在数据库中发生了更改,但网格显示之前的图片我已经删除了。我必须注销并再次登录才能看到更改。有没有办法克服这个问题?每次重新加载网格时有什么方法可以清除缓存?

【问题讨论】:

  • 尝试刷新页面。不是最佳做法,但我认为会奏效。

标签: jquery asp.net-mvc asp.net-mvc-3 c#-4.0


【解决方案1】:

您可以将持续时间用于项目中的特定捕获:

看看这个: http://dotnet.dzone.com/articles/programmatically-clearing-0

如果缓存是在客户端使用 Jquery-ajax 形成的,那么使用::

$.ajax({
    cache: false
    //rest of your ajax setup
});

【讨论】:

    【解决方案2】:

    您可以尝试在下面的帖子中编写自己的属性,例如 [no-cache],并可以在控制器上使用它来防止缓存。

    Prevent Caching in ASP.NET MVC for specific actions using an attribute

    http://dotnetnsqlcorner.blogspot.co.uk/2012/09/disable-caching-for-controller-action.html?m=1

    要禁用客户端缓存,您还可以在操作结果上使用 [outputcacheattattribute]。下面的帖子可能会有所帮助。

    [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] 
    

    How can I disable client side and proxy caching in ASP.NET MVC?

    还有其他禁用缓存的方法,但我想上述两种方法中的一种应该可以完成这项工作。希望这会有所帮助

    【讨论】:

      【解决方案3】:

      除了cache: false 在您的ajax 调用中,您还可以定位您的操作方法并创建一个无缓存操作过滤器属性。

      [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
      public sealed class NoCacheAttribute : ActionFilterAttribute
      {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
           filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
           filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
           filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
           filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
           filterContext.HttpContext.Response.Cache.SetNoStore();
      
           base.OnResultExecuting(filterContext);
        }
      }
      

      这很好解释Here。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-30
        • 2021-10-19
        • 1970-01-01
        • 2019-03-18
        • 1970-01-01
        • 2011-07-25
        • 2017-04-06
        相关资源
        最近更新 更多