servicestack,single-page-application,Asp.net Mvc 4,servicestack,Single Page Application" /> servicestack,single-page-application,Asp.net Mvc 4,servicestack,Single Page Application" />

Asp.net mvc 4 在没有关联控制器的情况下为cshtml文件启用缓存

Asp.net mvc 4 在没有关联控制器的情况下为cshtml文件启用缓存,asp.net-mvc-4,servicestack,single-page-application,Asp.net Mvc 4,servicestack,Single Page Application,我正在使用ServiceStack作为API提供程序开发单页应用程序(SPA),有两个cshtml页面可用,index.cshtml和admin.cshtml。这些文件是cshtml而不是简单html的唯一原因是利用了Asp.Net System.Web.Optimization 考虑到我没有可用的控制器,我正试图找出如何启用输出缓存。我知道如果我有控制器,我会使用OutputCache属性 我也知道这个应用程序需要重构,但为了时间起见,我需要让它在当前的结构中工作。任何帮助都将不胜感激 另外,

我正在使用ServiceStack作为API提供程序开发单页应用程序(SPA),有两个cshtml页面可用,index.cshtml和admin.cshtml。这些文件是cshtml而不是简单html的唯一原因是利用了Asp.Net System.Web.Optimization

考虑到我没有可用的控制器,我正试图找出如何启用输出缓存。我知道如果我有控制器,我会使用OutputCache属性

我也知道这个应用程序需要重构,但为了时间起见,我需要让它在当前的结构中工作。任何帮助都将不胜感激


另外,我还试图避免覆盖输出标题,因为我不想自己管理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])
公共静态无效
输出缓存
(此System.Web.HttpResponseBase响应,
int numberOfSeconds,[bool slideing=false],
System.Collections.Generic.IEnumerable varyByParams=null],
[System.Collections.Generic.IEnumerable varyByHeaders=null],
[System.Collections.Generic.IEnumerable varyByContentEncodings=null],
[System.Web.HttpCacheability cacheability=System.Web.HttpCacheability.Public])

这很简单。Visual Studio似乎有语法问题。。。但它起作用了。非常感谢。