的OutputCache属性和jQuery的Ajax不缓存 [英] OutputCache attribute and jQuery Ajax not caching

查看:180
本文介绍了的OutputCache属性和jQuery的Ajax不缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单MVC3控制器动作像这样

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

和我叫它从jQuery的阿贾克斯这样的

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

的问题是,该日期是总是当前日期,而不是​​缓存的响应。我的理解是, [的OutputCache(位置=服务器)] 表示服务器(MVC应用程序)缓存响应,当客户端请求数据时,动作拦截等等为不打扰与 DateTime.Now ,但返回缓存的响应。

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?

更新:

3nigma的答案是正确的。 VaryByParams =无的伎俩,但是.....这是一个从我的方法很明显,我没有任何参数,所以我为什么要需要说没有 。原来,PARAMS我在想的文档提到在我的方法PARAMS其实并不在我的方法的参数,可以它们是什么,请求处理程序可以作为诠释PARAMS。

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.

借助 MS文档

在此属性设置为多个参数,输出缓存
  包含每个不同版本的请求文档的
  指定的参数。可能的值包括无,*和任何
  有效的查询字符串
或POST参数名称。

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.

请参阅粗体(我的重点)的位,这意味着,虽然我不期待任何查询字符串参数,如果有的话得到发送(如jQuery.ajax做缓存时的方式:通过追加到请求的假 GET /公共/ GetTheDate?_ = 1324047171837 ),那么有一个参数,无论我期待这一点。

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.

推荐答案

缓存:假的,您明确告诉jQuery的不高速缓存设置缓存:真,

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

修改

设置的VaryByParam =无

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

这篇关于的OutputCache属性和jQuery的Ajax不缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆