MVC 3和Ajax调用 - 不能阻止它缓存 [英] MVC 3 and ajax calls - can't stop it caching

查看:85
本文介绍了MVC 3和Ajax调用 - 不能阻止它缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题的部分意见被缓存,我不想要的。我用Google搜索这一点,并有解决这个几个方面,其中没有一个似乎为我工作。

I am having problems with partial views being cached that I do not want to be. I have googled this and there are several ways around this, none of which seem to work for me.

基本上我有一个返回局部视图在服务器上的操作。我把它首先从Html.RenderPartial在页面加载。然后将所有其他调用从jQuery的同一页面,当链接被点击,然后刷新了页面的局部视图的节是on.Each链接是pressed发送不同的数据,所以德第一次每链接被点击调用TEH服务器,但以后每点击从缓存副本渲染。

Basically I have an action on the server that returns a partial view. I call it firstly from the Html.RenderPartial on page load. And then all the other calls are from jQuery on the same page, when links are clicked, which then refresh the section of the page the partial view is on.Each link that is pressed sends different data, so teh first time each link is clicked it calls teh server, but every subsequent click is rendered from a cached copy.

在我_layout页(我的母版页),我有以下的德顶部:

On my _Layout page (my master page) I have the following at teh top:

<script type="text/javascript">
        $.ajaxSetup({
            cache: false
        });
</script>

和每个AJAX调用有这样禁用缓存:

And each ajax call has the cache disabled like this:

$.ajax({
        url: UrlToGetEmployeeGrid,
        type: 'GET',
        cache: 'false',
        success: function (response) {
            $EmployeeGridObject.html(response);
        },
        error: function (xhr) {
            alert('There was an error contacting the server to refresh the employee grid');
        }
    });

这是从来没有到达服务器的要求。但我也尝试添加(以防万一)以下的控制器操作:

It is never reaching the server for the calls. But I also tried to add (just in case) the following to the controller action:

[HttpGet]
[OutputCache(Duration = 0)]

但是,这给了我一个错误:

But this gives me an error:

System.InvalidOperationException: Duration must be a positive number.

我也试过:

[HttpGet]
[OutputCache(Duration = 0, VaryByParam="none")]

和得到了同样的错误。

任何想法如何,我可以停止Ajax调用缓存的动作是否正确?

Any ideas how I can stop caching from ajax calls to an action properly?

更新:

这是保罗的评论继,这工作正常的Chrome浏览器,但在IE无法正常工作。我添加了一个随机数来解决这个问题,这一个特殊的情况。不过,长期来看,我需要一个更好的解决方案,因为会出现在那里我需要缓存的动作,一时间,IE浏览器将需要能够做的情况下。

Following on from Paulo's comment, this works fine in Chrome, but doesn't work in IE. I have added a random number to get around this issue for this one particular case. However, long term I need a better solution, as there will be cases where I need to cache actions for a time, that IE would need to be able to do.

推荐答案

您可以创建自己的NoCacheAttribute是这样的:

You can create your own NoCacheAttribute like this:

public class NoCacheAttribute : FilterAttribute, IActionFilter
{
    #region IActionFilter Members
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        return;
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();
    }
    #endregion
}

比你可以将它应用到你的行动:

Than you can apply it to your action:

[HttpGet]
[NoCache]

如果解决你的问题(我就是用这个解决方案在相当长的时间了)。

Should solve your problem (I was using this solution for quite long time now).

这篇关于MVC 3和Ajax调用 - 不能阻止它缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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