【问题标题】:how can cache "NotFound" page in asp .net core如何在 asp .net core 中缓存“NotFound”页面
【发布时间】:2021-08-12 11:14:38
【问题描述】:

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

在startup.cs中:

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

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

【问题讨论】:

    标签: c# asp.net asp.net-core caching asp.net-core-3.1


    【解决方案1】:

    你可以先安装这个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);
        }
    

    结果:

    【讨论】:

    • 感谢您的回答。我搜索你的方式,它是 server-side cache 。我使用它,但我也需要 浏览器缓存。因此,通过更多搜索,我发现在 notfound() 操作之前使用以下代码,我也可以将其缓存在浏览器中:[ResponseCache(VaryByHeader = "User-Agent", Duration = '<Time>')].
    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 2018-02-05
    • 2019-12-20
    • 2021-09-29
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    相关资源
    最近更新 更多