如何在 asp .net 核心中缓存 "NotFound" 页面

how can cache "NotFound" page in asp .net core

我的项目中有一个自定义 NotFound 页面,每个错误的 URL 都通过以下代码重定向到它:

In startup.cs:

app.UseStatusCodePagesWithRedirects("/not-found");

我想通过 cache policy 提供此静态页面。我该怎么做?

你可以先安装这个nuget包:

WebEssentials.AspNetCore.OutputCaching

然后在startup.cs中添加这些代码:

services.AddOutputCaching(); //ConfigureServices()

app.UseOutputCaching();  //Configure()

最后,将 OutputCache 属性添加到您的 notfound() 操作,然后它的输出将被缓存:

[OutputCache(Duration = 6000)] //cache 6000 seconds
    public IActionResult notfound()
    {
        return View(DateTime.Now);
    }

结果: