简体   繁体   English

在ASP.NET MVC中调试AJAX缓存

[英]Debugging AJAX caching in ASP.NET MVC

I have a simple proof of concept that seems to be handling caching oddly. 我有一个简单的概念证明,似乎在处理缓存。 Here's the view: 这是视图:

<script>       
$('#clickToLoad').click(function() {
    $.ajax({
        url: "<%=ResolveUrl("~/Home/AjaxCacheTest") %>?"
        , dataType: 'json'
            ,ifModified: true
            ,cache: true
        ,success: function(sourceData) {
        }
    });
});
</script>

This is linked to an action in the controller: 这链接到控制器中的动作:

[OutputCache(VaryByParam = "none", Duration = 3000)]
public ContentResult AjaxCacheTest()
{
    return Content("0", "application/json");
}

I'd like to get a result with caching, however, it seems that AjaxCacheTest doesn't cache unless I request it on its own (that is, in a browser window, instead of AJAX). 我想通过缓存获得结果,但是,除非我自己请求(即在浏览器窗口而不是AJAX中),否则AjaxCacheTest似乎不会缓存。

Setting cache: true doesn't help. 设置缓存:true无济于事。 Setting ifModified: true does cause caching, but then it doesn't call the success function, so I can't use the results. 设置ifModified:true会导致缓存,但是它不会调用成功函数,因此无法使用结果。

Are there any options that allow caching and still call the response callback? 是否有任何选项允许缓存并仍调用响应回调?

UPDATE: Thanks, forgot to add those. 更新:谢谢,忘了添加这些。 I'm testing with firebug and determining how caching is handled by a combination of looking at the response code (200 OK vs 302) and the response time for the request. 我正在测试Firebug,并通过查看响应代码(200 OK vs 302)和请求的响应时间来确定如何处理缓存。 This is all on Visual Studio's built in server (Cassini?). 所有这些都在Visual Studio的内置服务器(卡西尼(Cassini?))上。

Here's how the caching in your scenario works. 这是您方案中的缓存的工作方式。 Suppose you define the following action 假设您定义以下操作

[OutputCache(VaryByParam = "none", Duration = 5)]
public ActionResult AjaxCache()
{
    return Json(new { date = DateTime.Now }, JsonRequestBehavior.AllowGet);
}

If you don't explicitly set the Location attribute it default to OutputCacheLocation.Any which means: 如果未显式设置Location属性,则默认为OutputCacheLocation.Any ,这意味着:

The output cache can be located on the browser client (where the request originated), on a proxy server (or any other server) participating in the request, or on the server where the request was processed. 输出缓存可以位于浏览器客户端(发出请求的位置)上,参与请求的代理服务器(或任何其他服务器)上或处理请求的服务器上。 This value corresponds to the HttpCacheability.Public enumeration value. 此值对应于HttpCacheability.Public枚举值。

Duration = 5 means that the contents will be cached for 5 seconds. Duration = 5表示内容将被缓存5秒。 Next you send ajax request: 接下来,您发送ajax请求:

<%= Html.ActionLink("cache test", "AjaxCache") %>

<script type="text/javascript">
$(function() {
    $('a').click(function() {
        $.ajax({ url: this.href,
            success: function(json) {
                $('#result').html(json.date);
            }
        });        
        return false;
    });
});
</script>

<div id="result"></div>

If you click on the link twice during 5 seconds interval you will see that the result div will not change neither the server action will be hit but the success callback will always execute. 如果在5秒间隔内两次单击链接,您将看到结果div不会改变,服务器动作也不会被命中,但success回调将始终执行。 The server sends the following headers: 服务器发送以下标头:

Cache-Control: public, max-age=5
Expires: Fri, 26 Feb 2010 14:09:02 GMT
Last-Modified: Fri, 26 Feb 2010 14:08:57 GMT
Vary: *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM