I am trying to figure out why my HttpRuntime.Cache collection is always empty when controller action is invoked by $.ajax? In actions invoked normally everything works perfectly and I can get my data from Cache. Maybe actions invoked by AJAX have some different lifecycle and Cache collection is not ready yet? This is my example code:
action:
[HttpPost]
public PartialViewResult SomeAjaxAction()
{
// when receive ajax request HttpRuntime.Cache is always empty, but it shouldn't be
SomeType item = HttpRuntime.Cache.Get("cache_key") as SomeType;
return PartialView("SomeAjaxActionView", item);
}
invocation:
$.ajax({
type: 'POST',
url: '/controller/SomeAjaxAction/',
success: function (response) {
alert(response);
}
});
Is there any way to fix it e.g by custom action filter?
ANSWER
Ok, I found answer here: Access to session when making ajax call to Asp.net MVC action So I imporoved my code with base.HttpRuntime.Cache.Get("key") and it works as expected!
Ok, I found answer here: Access to session when making ajax call to Asp.net MVC action So I imporoved my code with
base.HttpRuntime.Cache.Get("key")and it works as expected!