我正在开发一个使用 ServiceStack 作为 API 提供者的单页面应用程序 (SPA),有两个可用的 cshtml 页面,index.cshtml 和 admin.cshtml。这些文件是 cshtml 而不是简单 html 的唯一原因是利用了 Asp.Net System.Web.Optimization。
考虑到我没有可用的 Controller ,我正在尝试弄清楚如何启用输出缓存。我知道如果我有 Controller ,我会使用 OutputCacheAttribute。
我也明白该应用程序需要重构,但出于时间考虑,我需要使其在当前结构中运行。任何帮助将不胜感激。
附注我还试图避免覆盖输出 header ,因为我不想自己管理 ETag。
最佳答案
您可以使用 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])
关于asp.net-mvc-4 - 为没有关联 Controller 的 cshtml 文件启用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17798098/