几个小时以来一直在努力解决这个问题。这是我要解决的问题:
我有这个使用 CacheProfile 的控制器/动作:
[DonutOutputCache(CacheProfile = "CachedAction")]
[ChildActionOnly]
public ActionResult ListOrders(string id, string selectedOrders)
{
}
这是我的 web.config 设置:
<caching>
<outputCache enableOutputCache="true" />
<outputCacheSettings>
<outputCacheProfiles>
<add name="CachedAction" duration="14100" varyByParam="id;selectedOrders" location="Any" />
</outputCacheProfiles>
</outputCacheSettings>
到目前为止一切正常,缓存按预期工作!
问题出在我的页面上,我有一个小“刷新按钮”,用户可以单击它来获取最新数据。为此,我只是在用户点击刷新后从页面调用 $.ajax(),但我调用了另一个操作,因为如果我调用原始 ListOrders,我只会得到它的缓存副本。
$.ajax({
url: '/controller/myajaxrefreshaorders/1?selectedOrders=xxxx',
type: "GET",
async:true,
cache: false,
这是我的问题。如果您看到我只是想破坏缓存并重定向到原始操作,它应该只是返回最新数据并更新缓存。但不管我做什么,它都不起作用!!!
public ActionResult MyAjaxRefreshOrders(string id, string selectedOrders)
{
var Ocm = new OutputCacheManager();
Ocm.RemoveItem("Controller", "ListOrders", new { id = id, selectedOrders= selectedOrders });
Response.RemoveOutputCacheItem(Url.Action("ListOrders", "Controller", new { id = id, selectedOrders = selectedOrders }));
return RedirectToAction("ListOrders", new { id = id, selectedOrders = selectedOrders });
}
事实上,这是我对现实中发生的事情的观察:
- 如果我继续重新加载页面,缓存工作正常,它显示最后一次检索项目的时间戳,这很棒。
- 如果我点击那个 ajaxrefresh 按钮,它会转到服务器,通过我的 cachebust 代码并简单地返回......即返回 RedirectToAction("ListOrders") 的调用永远不会进入该函数。
- 最后,ajaxcall 似乎为我创建了另一个缓存版本的操作。因此,在 ajax 调用完成后显示的时间戳是不同的时间戳,而当我重新加载页面时显示的时间戳也不同。
任何人有任何想法我做错了什么?我将非常感谢您的帮助,因为这让我发疯了!