我已使用为ASPX页面启用页面缓存
<%@ OutputCache Duration="7200" VaryByParam="*" Location="Server" %>
但是,下次重新生成页面时,如果页面中恰好出现错误,则该错误也会被缓存,并且网站会在接下来的7200秒内继续显示包含错误的页面,或者直到某个相关性刷新缓存为止。
目前,我尝试将站点错误日志添加为文件依赖性,以便在记录错误时刷新页面。但是,即使网站中的另一个页面出现错误,也会刷新页面。
问题是,我如何在错误处理块中放入一段代码来取消缓存当前页面。。
伪代码。
try
{
page load
}
catch (Exception ex)
{
// Add C# code to not cache the current page this time.
}
您可以简单地使用HttpResponse.RemoveOutputCacheItem
,如下所示:
try
{
//Page load
}
catch (Exception ex)
{
HttpResponse.RemoveOutputCacheItem("/mypage.aspx");
}
请参阅:是否有清除/刷新/删除OutputCache的方法?
另一种在Application_Error
中捕获异常并使用Response.Cache.AddValidationCallback
的方法是:
public void Application_Error(Object sender, EventArgs e) {
...
Response.Cache.AddValidationCallback(
DontCacheCurrentResponse,
null);
...
}
private void DontCacheCurrentResponse(
HttpContext context,
Object data,
ref HttpValidationStatus status) {
status = HttpValidationStatus.IgnoreThisRequest;
}