OutputCache 属性和 jQuery Ajax 不缓存

发布于 2024-12-21 19:31:56 字数 1514 浏览 3 评论 0原文

我有一个像这样的简单 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 应用程序)缓存响应,当客户端请求数据时,该操作将被拦截,以免打扰 < code>DateTime.Now 但返回缓存的响应。

我是否理解错误或者只是做错了什么?

更新:

3nigma的答案是正确的。 VaryByParams="none" 可以解决问题,但是......从我的方法中可以明显看出我没有任何参数,所以为什么我需要说“none”。事实证明,我认为文档中提到的“参数”是我方法中的参数,但实际上不是我方法中的参数,它们是请求处理程序可以解释为参数的任何内容。

MS 文档

当该属性设置为多个参数时,输出缓存 每个包含所请求文档的不同版本 指定参数。可能的值包括“none”、“*”和any 有效的查询字符串或 POST 参数名称。

请参阅粗体部分(我的重点),这意味着虽然我不期望任何查询字符串参数,但如果有任何参数被发送进来(就像 jQuery.ajax 在缓存时所做的那样:通过附加到请求 GET /Public /GetTheDate?_=1324047171837 )然后有一个参数,无论我是否期望它。

I have a simple MVC3 Controller Action like this

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

And I call it from jQuery Ajax like this

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); }
        });

The problem is that the date is always the current date, not the cached response. My understanding was that [OutputCache( Location=Server)] means that the server (the MVC app) caches the response and when the client requests the data, the action is intercepted so as not to bother with DateTime.Now but returns the cached response.

Am I understanding it wrongly or simply doing something wrong?

Update :

3nigma's answer is right. VaryByParams="none" does the trick but..... It's obvious from my method that I don't have any parameters so why should I need to say "none". Turns out that the 'Params' I was thinking the documentation referred to were params in my method are actually not the params in my method, they are anything that the request handler could construe as params.

The MS documentation says

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.

See the bit in bold (my emphasis) that means that although I'm not expecting any querystring parameters, if any get sent in (like the way jQuery.ajax does when cache:false by appending to the request GET /Public/GetTheDate?_=1324047171837 ) then there is a parameter, whether I expected it or not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2024-12-28 19:31:56

使用 cache: false, 您明确告诉 jquery 不要缓存设置 cache: true,

编辑

设置 VaryByParam="none" 就像

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

with cache: false, you are explicitly telling jquery not to cache set cache: true,

Edit

set the VaryByParam="none" like

[OutputCache(Duration=1200, VaryByParam="none",Location=System.Web.UI.OutputCacheLocation.Server)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文