已经与这一个小时的苦苦挣扎。以下是我试图解决:如何胸围为特定的控制器/动作/清晰的OutputCache在ASP.NET MVC
我有这样的控制器/动作它采用了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 });
}
其实这是我在现实中发生的观察:
- 如果我一直在重新加载页面,缓存工作正常,它显示的项目被检索的最后时间的时间戳,这很棒。
- 如果我打的ajaxrefreshbutton,它确实去到服务器,经过我的cachebust代码,只是返回back..i.e调用返回RedirectToAction(“ListOrders”)将不会进入该功能。
- 最后,看来ajaxcall会为我创建另一个动作的缓存版本。所以,在ajax调用完成后显示的时间戳是一个不同的时间戳,并且显示我何时重新加载页面的时间戳是不同的。
任何人有任何想法,我究竟做错了什么?我会真心感谢你的帮助,因为这让我疯狂!