0

我已经为我的 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.

}
4

1 回答 1

0

您可以简单地使用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;
}
于 2021-07-07T20:20:22.600 回答