【问题标题】:With ASP.Net, how do I enable browser caching for static content and disable it for dynamic content?使用 ASP.Net,如何为静态内容启用浏览器缓存并为动态内容禁用它?
【发布时间】:2011-09-23 08:43:07
【问题描述】:

我找到了很多关于让浏览器避免缓存动态内容(例如 .aspx 页面)的有用信息,但是我没有成功让浏览器缓存我的静态内容,特别是 css、javascript 和图像文件.

我一直在 Global.asax 中使用 Application_BeginRequest,但没有成功。拥有一个单独的静态内容服务器不是我们的选择。我还想避免必须配置 IIS 设置,除非它们可以从 web.config 进行控制。禁用 aspx 页面的缓存会影响其上显示的静态内容的缓存吗?

如果这个问题之前已经回答过,我深表歉意。

作为讨论的起点,这里是我的 Global.asax 文件的代码。

public class Global_asax : System.Web.HttpApplication
{
    private static HashSet<string> _fileExtensionsToCache;

    private static HashSet<string> FileExtensionsToCache
    {
        get 
        {
            if (_fileExtensionsToCache == null) 
            {
                _fileExtensionsToCache = new HashSet<string>();

                _fileExtensionsToCache.Add(".css");
                _fileExtensionsToCache.Add(".js");
                _fileExtensionsToCache.Add(".gif");
                _fileExtensionsToCache.Add(".jpg");
                _fileExtensionsToCache.Add(".png");
            }

            return _fileExtensionsToCache;
        }
    }

    public void Application_BeginRequest(object sender, EventArgs e)
    {
        var cache = HttpContext.Current.Response.Cache;

        if (FileExtensionsToCache.Contains(Request.CurrentExecutionFilePathExtension)) 
        {
            cache.SetExpires(DateTime.UtcNow.AddDays(1));
            cache.SetValidUntilExpires(true);
            cache.SetCacheability(HttpCacheability.Private);
        } 
        else 
        {
            cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            cache.SetValidUntilExpires(false);
            cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            cache.SetCacheability(HttpCacheability.NoCache);
            cache.SetNoStore();
        }
    }
}

【问题讨论】:

    标签: c# asp.net asp.net-4.0


    【解决方案1】:

    如果您使用的是 IIS7 & 并且想要缓存静态内容,请按照 documentation 在 web.config 中添加以下内容:

    <configuration>
      <system.webServer>
        <staticContent>
          <clientCache httpExpires="Sun, 27 Sep 2015 00:00:00 GMT" cacheControlMode="UseExpires" />
        </staticContent>
      </system.webServer>
    </configuration>
    

    【讨论】:

    • 太棒了!也简单多了。我之前遇到的问题(从 Application_BeginRequest 控制缓存)是否与使用 Visual Studio 开发服务器有关?另外,动态内容有类似的标签吗?
    • 我认为 Visual Studio 开发服务器与它没有任何关系。你打算使用页面缓存吗?如果是,您可以使用 web.config 设置控制持续时间。
    • 我想确保动态内容根本不被缓存。 Application_BeginRequest 中的代码很好地做到了这一点。我已将 .axd 添加到 _fileExtensionsToCache,这似乎可以防止他们的浏览器缓存设置在此处被覆盖。只是询问是否有更好的方法来做到这一点,即在 web.config 中。
    • :) :) 你的缓存今天过期了.. 机会是什么:)
    • @labroo - 哈哈!我什至不记得写过答案——确切的日期可能是在过去卡在某个地方的实时服务器上,哎呀。 :)
    【解决方案2】:

    魔法是由 HTTP 标头创造的 - 请参阅 page

    【讨论】:

    • 不应该在“cache.SetExpires(DateTime.UtcNow.AddDays(1))”这行添加过期日期到响应头吗?
    • 是的,应该的,可能问题出在 SetCacheAbility 方法上,见codeclimber.net.nz/archive/2007/04/01/…
    • 好文章。谢谢!
    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 2014-08-30
    • 2014-07-27
    • 2017-12-17
    相关资源
    最近更新 更多