OutputCache不起作用

Pup*_*kin 6 c# asp.net-mvc caching

我想缓存操作返回的数据。我OutPutCacheAttribute为此使用。这是我的客户代码:

$(document).ready(function() {
    $.get('@Url.Action("GetMenu", "Home")', null, 
        function(data) {
            parseMenu(data);                  
    });
}
Run Code Online (Sandbox Code Playgroud)

这是我的服务器代码:

[HttpGet]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.Server)] 
public ContentResult GetMenu()
{
    string jsonText = GetData(); //some code
    return new ContentResult
    {
        Content = jsonText,
        ContentType = "text/json"
    };
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我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);
}
Run Code Online (Sandbox Code Playgroud)

我以为如果OutputCache属性正常工作(并且正确使用了它),那么操作将被调用一次,并且每次都会得到相同的响应。但是如果不是这样,我每次都会得到不同的响应,因为每次都会生成随机数。当我几次调用此操作时,我总是收到不同的响应,例如{"random":36}{"random":84}等等。

Ash*_*man 0

请尝试这个

  [OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient,VaryByParam = "none")]
Run Code Online (Sandbox Code Playgroud)

如果不起作用请尝试:

        [HttpGet]
    [OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "none")]
    public JsonResult GetJson()
    {
        return Json(new{message = "Record created successfully!!!"},JsonRequestBehavior.AllowGet);
    }
Run Code Online (Sandbox Code Playgroud)

注意:有关输出缓存的更多信息