我想緩存動作的返回數據。爲此我使用OutPutCacheAttribute
。這裏是我的客戶端代碼:OutputCache不起作用
$(document).ready(function() {
$.get('@Url.Action("GetMenu", "Home")', null,
function(data) {
parseMenu(data);
});
}
這裏是我的服務器代碼:
[HttpGet]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.Server)]
public ContentResult GetMenu()
{
string jsonText = GetData(); //some code
return new ContentResult
{
Content = jsonText,
ContentType = "text/json"
};
}
正如你可以看到我使用OutputCacheAttribute
緩存服務器響應。但它不起作用。每次加載頁面時,都會調用Home/GetMenu
行動。即使直接在瀏覽器的地址欄中輸入'localhost/Home/GetMenu',也會被調用。我在哪裏弄錯了?
UPD我創建了第二個動作來測試此屬性而不進行調試。下面是它的代碼:
[HttpGet]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "none")]
public JsonResult GetJson()
{
return Json(new
{
random = new Random().Next(100)
},
JsonRequestBehavior.AllowGet);
}
我認爲,如果屬性的OutputCache工作正常(我用它正確),那麼動作被調用一次,每次都遇到同樣的反應。但是,如果沒有,那麼我每次都會得到不同的答案,因爲每次都會產生隨機數。 當我叫這個動作有些時候我總是收到不同respnses,如{"random":36}
,{"random":84}
等
有你解決了這個@Pupkin?我想知道原因是什麼。 –
@TorbjörnHansson傷心,但我還沒有解決這個問題 – Pupkin