加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – OutputCache属性和jQuery Ajax没有缓存

发布时间:2020-12-16 06:23:50 所属栏目:asp.Net 来源:网络整理
导读:我有一个像这样的简单的MVC3控制器动作 [HttpGet][OutputCache(Duration = 1200,Location=System.Web.UI.OutputCacheLocation.Server)]public string GetTheDate(){ return DateTime.Now.ToString();} 我从这样的jQuery Ajax中调用它 jQuery.ajax({ type: "G
我有一个像这样的简单的MVC3控制器动作

[HttpGet]
[OutputCache(Duration = 1200,Location=System.Web.UI.OutputCacheLocation.Server)]
public string GetTheDate()
{
    return DateTime.Now.ToString();
}

我从这样的jQuery Ajax中调用它

jQuery.ajax({
            type: "GET",url: "http://localhost:60690/Public/GetTheDate",cache: false,success: function (data) {
                //alert("success");
                jQuery("#stats").append("<b>" + data + "</b>");
            },error: function (req,status,error) { alert("failure"); alert(error + " " + status + " " + req); }
        });

问题是日期始终是当前日期,而不是缓存的响应.我的理解是[OutputCache(Location = Server)]意味着服务器(MVC应用程序)缓存响应,当客户端请求数据时,拦截操作以免打扰DateTime.Now但返回缓存的响应.

我是错误地理解它还是只是做错了什么?

更新:

3nigma的答案是对的. VaryByParams =“none”可以解决这个问题但是……从我的方法中可以明显看出我没有任何参数,所以我为什么要说“无”.事实证明,’Params’我认为所提到的文档是我方法中的params实际上不是我方法中的参数,它们是请求处理程序可以解释为params的任何东西.

MS documentation说

When this property is set to multiple parameters,the output cache
contains a different version of the requested document for each
specified parameter. Possible values include “none”,“*”,and any
valid query string
or POST parameter name.

请参阅粗体位(我的重点),这意味着虽然我不期望任何querystring参数,如果有任何get发送(就像jQuery.ajax在缓存时所做的那样:false通过附加到请求GET / Public / GetTheDate? _ = 1324047171837)然后有一个参数,无论我是否预期.

解决方法

with cache:false,你明确告诉jquery不要缓存set cache:true,

编辑

设置VaryByParam =“无”之类的

[OutputCache(Duration=1200,VaryByParam="none",Location=System.Web.UI.OutputCacheLocation.Server)]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读