我一直在寻找有关如何在项目级别上禁用客户端缓存的信息。 我知道我可以在操作方法之前添加以下内容:
[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
我也读到过关于为缓存制作配置文件的内容,但那将意味着在多个地方引用它们。我想在web.config中或者在IIS中设置单一的设置。
我正在处理包含大量局部视图的项目
提前感谢您对此事提供的任何建议。
我一直在寻找有关如何在项目级别上禁用客户端缓存的信息。 我知道我可以在操作方法之前添加以下内容:
[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
我也读到过关于为缓存制作配置文件的内容,但那将意味着在多个地方引用它们。我想在web.config中或者在IIS中设置单一的设置。
我正在处理包含大量局部视图的项目
提前感谢您对此事提供的任何建议。
<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-store
到Cache-Control
(http://code.google.com/p/chromium/issues/detail?id=28035)
您可以在项目级别或子目录级别设置它们以控制浏览器缓存。例如,在主要是数据驱动/动态站点中,我可能会在项目级别设置这些头文件,但在包含我的.js、.css、图像的/static目录中,添加另一个web.config文件,其中包括适当的</clear>
指令,并且可能设置远期过期头文件。
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
。
希望这对你有所帮助 ;)
在@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>
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:24:00" />
</staticContent>
</system.webServer>
</configuration>
OutputCache
属性用于服务器端输出操作的输出缓存。要关闭它,只需不将属性应用于操作/控制器即可。如果您想禁用客户端,则可以通过添加一个头来通知浏览器不要缓存结果来实现。
您可以在web.config
中定义缓存配置文件,但是,在MVC 3中使用缓存配置文件似乎无法与OutputCache
属性配合使用。请阅读此问题:使用缓存配置文件缓存ChildActions无法工作?
试试这个
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]