【发布时间】: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