2013-07-22 52 views
3

我正在使用ServiceStack作爲API提供者的單頁應用程序(SPA)中,有兩個可用的cshtml頁面,index.cshtml和admin.cshtml。這些文件是cshtml而不是簡單的html的唯一原因是利用Asp.Net System.Web.Optimization。爲沒有關聯控制器的cshtml文件啓用緩存

我想弄清楚如何啓用輸出緩存,考慮到我沒有控制器可用。我知道如果我有控制器,我會使用OutputCacheAttribute。

我也明白,應用程序需要重構,但爲了時間的緣故,我需要使它在當前結構中工作。任何幫助將主要讚賞。

P.S.我也試圖避免覆蓋輸出標題,因爲我不想自己管理ETag。

回答

3

可以使用Response.OutputCache()擴展方法:

@{ 
    Response.OutputCache(30); 
} 

它有很多的參數,讓你可以,你會在屬性的OutputCache指定同樣的事情。

下面是文檔:

public static void 
OutputCache 
(this System.Web.HttpResponseBase response, 
int numberOfSeconds, [bool sliding = false], 
System.Collections.Generic.IEnumerable<string> varyByParams = null], 
[System.Collections.Generic.IEnumerable<string> varyByHeaders = null], 
[System.Collections.Generic.IEnumerable<string> varyByContentEncodings = null], 
[System.Web.HttpCacheability cacheability = System.Web.HttpCacheability.Public]) 
+0

這是很容易。 Visual Studio似乎有一個語法問題......但它的工作。謝謝 – user975060