返回 Json 的操作上的 OutputCache 属性不起作用 - 当我在浏览器中多次点击操作 URL 时,每次我在 VS2012 中激活断点(看起来 OutputCache 属性被忽略)。这是我的代码:
public class ApiController : GenericControllerBase
{
[OutputCache(Duration = 300, VaryByParam = "type;showEmpty;sort;platform")]
public JsonResult GetCategories(string type, bool? showEmpty, string sort, string platform)
{
///... creating categoryResults object
return Json(new ApiResult() { Result = categoryResults }, JsonRequestBehavior.AllowGet);
}
}
Run Code Online (Sandbox Code Playgroud)
GenericControllerBase 继承自 Controller。在从 GenericControllerBase OutputCache 继承的其他控制器中,按预期工作,但它们返回 View() 而不是 Json。作为一个实验,我添加了 VaryByCustom 参数并检查了 global.asax 文件中的 GetVaryByCustomString 方法也没有被命中,因此缓存功能被完全忽略。我使用 MVC3 和 autofac 进行服务注入(但其他使用 autofac 注入的控制器与 OutputCache 一起正常工作)。
可能是什么问题呢?什么会阻止 OutputCache 功能?它是否可能与缓存整个响应有关?在我的项目中使用 OutputCache 的所有其他操作都是在视图中嵌入 @Html.Action(...) …
我能够适当地利用ASP.NET MVC4 OutputCache和以下代码作为我的Controller的索引()的属性:
[OutputCache(Duration = 600, VaryByParam = "none")]
Run Code Online (Sandbox Code Playgroud)
然而,
为了管理不同的缓存场景,根据MSDN文档,我们可以使用OutputCache缓存(例如)我们的控制器Index()方法.在文档之后,我们最终得到以下代码:
在我们的控制器中
//[OutputCache(Duration = 600, VaryByParam = "none")] //works without any modification to the Web.config
[OutputCache(CacheProfile = "Cache1Hour", VaryByParam = "none")]
public ActionResult Index()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
而在我们的Web.config
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="Cache1Hour" duration="3600"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
...
...
...
</system.web>
Run Code Online (Sandbox Code Playgroud)
问题/问题: 显然我错过了一些东西,因为我不断抛出以下异常:
异常详细信息:System.Web.HttpException:未定义"Cache1Hour"缓存配置文件.请在配置文件中定义它.
完全例外:
Server Error in '/' Application.
The 'Cache1Hour' cache profile is not defined. Please define it in the …Run Code Online (Sandbox Code Playgroud) 使用而不是时通过ASP.NET RC刷新解决了此缺陷。VaryByParam=""VaryByParam="None"
缓存作品,但是页面http://www.yoursite.com和http://www.yoursite.com/home/index似乎将分别缓存。
VaryByParam=""和之间有什么区别VaryByParam="None"?
编辑:我的意思是VaryByParam=""不是VaryByParam =“ *”,因为该错误仍然存在与VaryByParam =“ *”
控制器操作上输出缓存的确切密钥格式是什么?
[OutputCache(CacheProfile = "Games")]
public virtual ActionResult EventGames(int? id, string slug)
Run Code Online (Sandbox Code Playgroud)