简体   繁体   English

ASP.NET Web 窗体应用程序的静态文件浏览器缓存

[英]Static File Browser Caching for ASP.NET Web Forms Application

We want browser caching on the content from certain folders on the site (images / styles / scripts) and setting them to remain fresh for 7 days.我们希望浏览器缓存网站上某些文件夹中的内容(图像/样式/脚本),并将它们设置为保持新鲜状态 7 天。 Also, we use a CMS called Ektron.此外,我们使用名为 Ektron 的 CMS。

Here is the code we were using, any ideas as to why this isn't working?这是我们使用的代码,关于为什么这不起作用的任何想法?

From global.asax来自 global.asax

void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
    System.Web.HttpCacheability cachelevel = System.Web.HttpCacheability.Private;

    if (Request.FilePath.ToLower().StartsWith("/images") || Request.FilePath.ToLower().StartsWith("/styles") || 
        Request.FilePath.ToLower().StartsWith("/js") || Request.FilePath.ToLower().StartsWith("/scripts"))
    {
        cachelevel = System.Web.HttpCacheability.Public;

        if (cachelevel == System.Web.HttpCacheability.Public) Response.Cache.SetCacheability(cachelevel);

        var staticExtensions = new List<string> { ".js", ".css", ".jpg", ".gif", ".png" };

        if (staticExtensions.Any(ext => Request.FilePath.ToLower().EndsWith(ext)))
        {
            Response.Cache.SetMaxAge(TimeSpan.FromDays(7));
        }
    }

Don't think Application_PreSendRequestHeaders will not fire for every static content.不要认为 Application_PreSendRequestHeaders 不会为每个静态内容触发。 Instead use the Web.config system.webServer - staticContent - clientCache setting, like this:而是使用 Web.config system.webServer - staticContent - clientCache 设置,如下所示:

<system.webServer>
 <staticContent>
  <clientCache cacheControlMaxAge="7.00:00:00" cacheControlMode="UseMaxAge" />
 </staticContent>

This will set it to 7 days这会将其设置为 7 天

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM