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

时间:2017-06-04 21:41:57

标签: .net caching asp.net-core-mvc environment

是否有一种简单的方法可以在生产环境中缓存MVC Core页面(View)?我尝试了下面的代码,但标签帮助程序不起作用,因为结束标记必须在标记内。

<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>

似乎没错?

2 个答案:

答案 0 :(得分:1)

您错过了缓存标记的结束。尝试在缓存标记后添加</cache>

示例:

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

答案 1 :(得分:0)

只需使用标记包装要缓存的内容,标记的内容将缓存在内存中。

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