【问题标题】:Can't Clear C# MVC Cache无法清除 C# MVC 缓存
【发布时间】:2018-06-28 14:37:39
【问题描述】:

我在我的 web.config 文件中使用此设置并且我的客户端浏览器缓存了我的 MVC 控制器方法结果?如何以及为什么?我的配置不针对 cshtml 或 Razor 视图。我现在可以做什么来清除我的客户端浏览器缓存?

<system.webServer>
<caching>
      <profiles>
        <add extension=".png" policy="CacheUntilChange" varyByHeaders="User-Agent" location="Client"/>
        <add extension=".gif" policy="CacheUntilChange" varyByHeaders="User-Agent" location="Client"/>
        <add extension=".jpg" policy="CacheUntilChange" varyByHeaders="User-Agent" location="Client"/>
        <add extension=".js" policy="CacheUntilChange" varyByHeaders="User-Agent" location="Client"/>
        <add extension=".css" policy="CacheUntilChange" varyByHeaders="User-Agent" location="Client"/>
      </profiles>
    </caching>
<httpProtocol allowKeepAlive="true">
      <customHeaders>
      <add name="Cache-Control" value="public, max-age=691200"/>
      </customHeaders>
</httpProtocol>

【问题讨论】:

    标签: c# caching model-view-controller browser-cache


    【解决方案1】:

    您可以通过编程方式进行,试试这个: 把它放在你的模型上:

    public class NoCache : 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);
        }
    }
    and on your specific controller: e.g:
    
    [NoCache]
    [Authorize]
    public ActionResult Home()
     {
         ////////...
    }
    

    代码取自: How to clear cache in specified controller in asp mvc?

    【讨论】:

    • 我的客户端浏览器已经缓存的页面。我必须在再次设置之前清除。
    【解决方案2】:

    一种解决方案是在项目中的 .css、.js、.gif 文件之后设置版本号。我知道这是对系统的巨大改变。这就是您可以清除客户端计算机上的缓存的方法

    类似的东西

    script.css?v=1.0 // This is the URL for release 1.0
    

    希望对您有所帮助。祝你好运。

    【讨论】:

    • 我说的不是css js之类的静态文件
    • 你也可以做其他文件。 jpg、gif等
    • 我的问题更具体。每个人都知道那个解决方案...添加问号和版本...
    猜你喜欢
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多