Been struggling with this one for hours now. Here is what I am trying to solve :
I have this controller/action which uses a CacheProfile:
[DonutOutputCache(CacheProfile = "CachedAction")]
[ChildActionOnly]
public ActionResult ListOrders(string id, string selectedOrders)
{
}
Here are my web.config settings :
<caching>
<outputCache enableOutputCache="true" />
<outputCacheSettings>
<outputCacheProfiles>
<add name="CachedAction" duration="14100" varyByParam="id;selectedOrders" location="Any" />
</outputCacheProfiles>
</outputCacheSettings>
Everything works great so far and caching works as expected !!
The problem is on my page I have a little "refresh button" which the user can click to get the latest data. For this, I just do a $.ajax() call from the page after the user hits the refresh, but I call another action because if I call the original ListOrders, I will just get a cached copy of it.
$.ajax({
url: '/controller/myajaxrefreshaorders/1?selectedOrders=xxxx',
type: "GET",
async:true,
cache: false,
And here is my problem. If you see I am just trying to bust the cache and redirect to the original action, which should simply return latest data and update the Cache. But no matter what I do, it is not working!!!
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 });
}
In fact here is my observation of what happens in reality :
- If I keep reloading the page, the cache works fine and it shows the timestamp of the last time the item was retrieved, which is great.
- If I hit that ajaxrefreshbutton, it does go to the server, goes through my cachebust code and simply returns back..i.e the call to return RedirectToAction("ListOrders") never enters that function.
- Finally, it appears that the ajaxcall creates another cached version of the action for me instead. So, the timestamp which shows up after the ajax call completes is a different time stamp and the time stamp which shows when I reload the page is different.
Anyone has any ideas what am I doing wrong?? I will truly appreciate your help as this is driving me nuts!