如何在 MVC Core 中缓存页面,仅在生产环境中

How to cache page in MVC Core, only in Production environment

是否有一种仅在生产环境中缓存 MVC 核心页面(视图)的简单方法?我尝试了下面的代码,但标签助手不起作用,因为结束标签必须在标签内。

<environment names="Staging,Production">
    <cache expires-after="@TimeSpan.FromMinutes(30)">
</environment>

可能使用 enabled 属性?

谢谢。

更新:

如果你想缓存整个页面,以避免有一个部分和一个没有它的部分(因为你仍然想要在开发环境中的内容),我想你可以将整个页面放入一个部分一些人指出的观点。那行得通。你最终会得到:

<environment names="Staging,Production">
    <cache expires-after="@TimeSpan.FromMinutes(30)">
        @Html.Partial("_PartialViewName")
        *last updated  @DateTime.Now.ToLongTimeString()
    </cache>
</environment>
<environment names="Development">
    @Html.Partial("_PartialViewName")
</environment>

看起来正确?

您缺少缓存标记的结尾。尝试在缓存标记后添加 </cache>

示例:

<environment names="Staging,Production">
    <cache expires-after="@TimeSpan.FromMinutes(30)">
        @DateTime.Now
    </cache>
</environment>

只需将要缓存的内容用标签包裹起来,标签的内容就会缓存到内存中。

<environment names="Staging,Production">
    <cache expires-after="@TimeSpan.FromMinutes(30)">
        @Html.Partial("_PartialViewName")
        *last updated  @DateTime.Now.ToLongTimeString()
    </cache>
</environment>