C# 在ASP.Net中,如何启用静态内容的浏览器缓存并禁用它以用于动态内容?

C# 在ASP.Net中,如何启用静态内容的浏览器缓存并禁用它以用于动态内容?,c#,asp.net,asp.net-4.0,C#,Asp.net,Asp.net 4.0,我发现了很多关于让浏览器避免缓存动态内容(例如,aspx页面)的好信息,但是我没有成功地让浏览器缓存我的静态内容,特别是css、javascript和图像文件 我一直在Global.asax中使用应用程序_BeginRequest,但没有成功。为静态内容提供单独的服务器不是我们的选择。我还希望避免必须配置IIS设置,除非它们可以从web.config进行控制。禁用aspx页面的缓存会影响显示在页面上的静态内容的缓存吗 如果之前已经回答过这个问题,我表示歉意 作为讨论的起点,下面是我的Global

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

我一直在Global.asax中使用应用程序_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();
        }
    }
}
公共类全局\u asax:System.Web.HttpApplication
{
私有静态HashSet_fileExtensionsToCache;
私有静态哈希集文件扩展目录
{
得到
{
if(_fileExtensionsToCache==null)
{
_fileExtensionsToCache=newhashset();
_fileExtensionsToCache.Add(“.css”);
_fileExtensionsToCache.Add(“.js”);
_fileExtensionsToCache.Add(“.gif”);
_fileExtensionsToCache.Add(“.jpg”);
_fileExtensionsToCache.Add(“.png”);
}
返回_fileExtensionsToCache;
}
}
public void应用程序_BeginRequest(对象发送方,事件参数e)
{
var cache=HttpContext.Current.Response.cache;
if(FileExtensionsToCache.Contains(Request.CurrentExecutionFilePathExtension))
{
SetExpires(DateTime.UtcNow.AddDays(1));
cache.SetValidUntilExpires(true);
SetCacheability(HttpCacheability.Private);
} 
其他的
{
SetExpires(DateTime.UtcNow.AddDays(-1));
cache.SetValidUntilExpires(false);
SetRevalidation(HttpCacheRevalidation.AllCaches);
SetCacheability(HttpCacheability.NoCache);
cache.SetNoStore();
}
}
}

如果您正在使用IIS7&并且希望缓存静态内容,请根据以下内容在web.config中添加以下内容:



这个魔法是由HTTP头实现的-请看这一点。

行“cache.SetExpires(DateTime.UtcNow.AddDays(1))”不应该将到期日期添加到响应头吗?是的,应该,可能问题出在SetCacheAbility方法中,请参阅尼斯文章。谢谢好极了简单多了。我以前遇到的问题(从应用程序_BeginRequest控制缓存)是否与使用Visual Studio Development Server有关?另外,动态内容是否也有类似的标签?我认为VisualStudioDevelopmentServer与此无关。您要使用页面缓存吗?如果是,您可以使用web.config设置来控制持续时间。我希望确保动态内容根本不被缓存。应用程序_BeginRequest中的代码很好地做到了这一点。我已经将.axd添加到了_fileExtensionsToCache中,这似乎可以防止它们的浏览器缓存设置在这里被覆盖。如果有更好的方法,也就是在web.config.:)中,您的缓存将在今天过期。。机会有多大:)@labroo-哈哈!我甚至记不起写下了答案——确切的日期可能是在一台实时服务器上的某个地方,在过去的某个地方,哎呀。:)
<configuration>
  <system.webServer>
    <staticContent>
      <clientCache httpExpires="Sun, 27 Sep 2015 00:00:00 GMT" cacheControlMode="UseExpires" />
    </staticContent>
  </system.webServer>
</configuration>