I want to cache return data from an action. I use the OutPutCacheAttribute
for this purpose. Here is my client code:
$(document).ready(function() {
$.get('@Url.Action("GetMenu", "Home")', null,
function(data) {
parseMenu(data);
});
}
And here is my server code:
[HttpGet]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.Server)]
public ContentResult GetMenu()
{
string jsonText = GetData(); //some code
return new ContentResult
{
Content = jsonText,
ContentType = "text/json"
};
}
As you can see I use OutputCacheAttribute
for caching server response. But it does not work. Every time I load the page the action Home/GetMenu
is called. And it is called even if I type in browser's address bar directly 'localhost/Home/GetMenu'. Where am I was mistaken?
UPD I created the second action to test this attribute without debugging. Here is its code:
[HttpGet]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "none")]
public JsonResult GetJson()
{
return Json(new
{
random = new Random().Next(100)
},
JsonRequestBehavior.AllowGet);
}
I supposed if OutputCache attribute works properly (and I use it properly) then action is called once and I get the same response every time. But if not then I get the different responses every time because every time random number is genereted.
And when I called this action some times I have always received different respnses, such as {"random":36}
, {"random":84}
and so on