禁用客户端缓存

bur*_*fon 14 asp.net-mvc iis-7 outputcache asp.net-mvc-3

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

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

我还阅读了有关为缓存创建配置文件的内容,但这也意味着在几个地方引用它们.我想在web.config中设置一个设置,或者在IIS中?

我正在研究的项目包含很多部分视图

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

Tom*_*Tom 30

您可以通过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>
Run Code Online (Sandbox Code Playgroud)

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

修改:已添加no-storeCache-ControlChrome(http://code.google.com/p/chromium/issues/detail?id=28035)

您可以在项目级别或子目录级别设置此项,以根据需要控制浏览器缓存.例如,在主要数据驱动/动态网站,我可以在项目级别设置这些标题,但在/静态目录(其中包含我的.js,的CSS,图像),添加其他的web.config其中包括适当的</clear>指令,或许可以设置一个far-future-expires头.


Wah*_*tar 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.
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我的所有控制器都继承自此BaseController而不是正常的Controller

希望这有帮助;)


fre*_*nky 0

您可以在 中定义缓存配置文件web.config,但是,使用缓存配置文件似乎不适用于OutputCachemvc 3 中的属性。阅读此问题:使用缓存配置文件缓存 ChildActions 不起作用?