禁用客户端缓存

19

我一直在寻找有关如何在项目级别上禁用客户端缓存的信息。 我知道我可以在操作方法之前添加以下内容:

[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

我也读到过关于为缓存制作配置文件的内容,但那将意味着在多个地方引用它们。我想在web.config中或者在IIS中设置单一的设置。

我正在处理包含大量局部视图的项目

提前感谢您对此事提供的任何建议。

7个回答

40
你可以通过Web.Config禁用浏览器缓存:

<configuration>
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Cache-Control" value="no-cache, no-store" />
                <add name="Pragma" value="no-cache" />
                <add name="Expires" value="-1" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

来源:http://blog.jamesjones.name/2009/11/how-to-disable-browser-caching-in.html

编辑:对于Chrome添加了no-storeCache-Controlhttp://code.google.com/p/chromium/issues/detail?id=28035

您可以在项目级别或子目录级别设置它们以控制浏览器缓存。例如,在主要是数据驱动/动态站点中,我可能会在项目级别设置这些头文件,但在包含我的.js、.css、图像的/static目录中,添加另一个web.config文件,其中包括适当的</clear>指令,并且可能设置远期过期头文件。


此更改可能需要在第一次生效前清除浏览器缓存。 - estinamir

5
你可以创建一个名为BaseController的类,并将缓存配置文件设置为它。 然后让所有的控制器都继承自这个BaseController

更新:

这是我已经有的内容:

// Here is my custom OutputCaheAttribute to prevent cache at all.
//Whatever you may put anything you want.
//Of course i don't use it here but i put it to show you how it's going.
[NoCache]
public class BaseController : Controller
{
    protected override ViewResult View(string viewName, string masterName, object model)
    {
        // I do some stuffs here to change MasterPage depending on current culture.
        // Don't care about it i just wanna show you why BaseController is good idea.
    }
}

那么所有我的控制器都继承自这个BaseController而不是普通的Controller

希望这对你有所帮助 ;)


谢谢您的评论Wahid!但是我不是被迫为每个ActionResult声明一个OutputCache属性吗?或者我可以在类级别上以某种方式做到这一点吗? - burktelefon
是的,您可以在控制器中设置缓存,然后该控制器中的所有操作都将采用相同的缓存设置。 - Wahid Bitar

1

@Tom的回答基础上,对于按文件或目录进行缓存破坏:

<configuration>
<!--  disable cache for every file in this directory -->
  <location path="dist">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="DisableCache" />
      </staticContent>
      <httpProtocol>
        <customHeaders>
          <add name="Cache-Control" value="no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0, s-maxage=0" />
          <add name="Pragma" value="no-cache" />
          <add name="Expires" value="-1" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>
  </location>
</configuration>

0
如果您需要将子文件夹中的文件缓存1天(24小时),则可以为这些子文件夹添加单独的web.config文件(第一次需要清除客户端缓存)。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <staticContent>
            <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:24:00" />
        </staticContent>
    </system.webServer>
</configuration>

0

OutputCache 属性用于服务器端输出操作的输出缓存。要关闭它,只需不将属性应用于操作/控制器即可。如果您想禁用客户端,则可以通过添加一个头来通知浏览器不要缓存结果来实现。


0

“不起作用”听起来有点严厉。它听起来像是对于子操作没有完全支持。 - bzlm
@bzlm 有什么区别吗?它不能用于子操作。 - frennky
即使您从配置文件中删除除“Duration”和“VaryByParam”之外的所有内容,也是如此吗? - bzlm
@bzlm 不太确定我理解了。名称是必需属性,因此无法删除它,删除启用属性也没有帮助。 - frennky

-1

试试这个

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接