2017-06-04 24 views
1

只有在生產環境中才有緩存MVC Core頁面(View)的簡單方法嗎?我嘗試了下面的代碼,但標籤助手不起作用,因爲結束標籤必須位於標籤內。如何僅在生產環境中在MVC Core中緩存頁面

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

可能使用啓用屬性?

謝謝。

UPDATE:

如果你希望緩存整個頁面,以避免一個部分,以及一個沒有它(因爲你仍然希望在開發環境中的內容),我會想你可以把整個頁面變成部分視圖,如某些人所述。這將工作。你最終會得到:

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

看起來正確嗎?

回答

1

你錯過了關閉緩存標記。嘗試在緩存標記後添加</cache>

例子:

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

簡單地包裹你想用一個標籤中緩存的內容和標籤的內容將被緩存在內存。

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

我會標記爲答案,因爲這提供的提示把你要顯示的內容,在所有環境中的局部視圖,則包括多個環境標籤,與和一個沒有它。 – user2737646

相關問題