如何停止MVC缓存调用的操作方法的结果? [英] How to stop MVC caching the results of invoking an action method?

查看:99
本文介绍了如何停止MVC缓存调用的操作方法的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了IE缓存的操作方法结果的问题。

I am experiencing a problem with IE caching the results of an action method.

我发现的其它文章中涉及到的安全性和[授权]属性。这个问题有无关的安全性。

Other articles I found were related to security and the [Authorize] attribute. This problem has nothing to do with security.

这是一个非常简单的录制票,抢平均,回报和平均投票数的方法。关于它的唯一稍微有趣的事情是,它通过Ajax调用,并返回一个JSON对象。我认为,这是越来越缓存JSON对象。

This is a very simple "record a vote, grab the average, return the avg and the number of votes" method. The only slightly interesting thing about it is that it is invoked via Ajax and returns a Json object. I believe that it is the Json object that is getting cached.

当我从Firefox运行它并观看与萤火虫的XHR交通,一切完美的作品。然而,IE 8下的活动指示器图形不曾经有时间显示出来,并且显示该网页的元素的正在注入页面用jQuery新平均和计数是从未不同。

When I run it from FireFox and watch the XHR traffic with Firebug, everything works perfectly. However, under IE 8 the "throbber" graphic doesn't ever have time to show up and the page elements that display the "new" avg and count that are being injected into the page with jQuery are never different.

我需要一种方法来告诉MVC永远不要缓存此操作方法。

I need a way to tell MVC to never cache this action method.

这篇文章似乎为解决这个问题,但我无法理解这一点:
<一href=\"http://stackoverflow.com/questions/1441467/$p$pvent-caching-of-attributes-in-asp-net-mvc-force-attribute-execution-every-tim\">$p$pvent在ASP.NET MVC属性的缓存,力量属性执行每一个Action执行

This article seems to address the problem, but I cannot understand it: Prevent Caching of Attributes in ASP.NET MVC, force Attribute Execution every time an Action is Executed

我需要更多的上下文解决方案,了解如何扩展AuthorizationAttribute。请将您的答案,如果你说话的人谁没有MVC的深刻理解,即使这意味着对一些基本/ $ P $的要求prequisites文章回复。

I need a bit more context for the solution to understand how to extend AuthorizationAttribute. Please address your answer as if you were speaking to someone who lacks a deep understanding of MVC even if that means replying with an article on some basics/prerequisites that are required.

谢谢,

卡罗尔的三分球

推荐答案

MVC不缓存的结果。 IE浏览器。

MVC doesn't cache the results. IE does.

所以,你要告诉IE无法做到这一点。

So you have to tell IE not to do it.

下面就是我如何做到这一点。首先,一个属性:

Here's how I do it. First, an attribute:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class CacheControlAttribute : ActionFilterAttribute
{
    public CacheControlAttribute(HttpCacheability cacheability)
    {
        this._cacheability = cacheability;
    }

    public HttpCacheability Cacheability { get { return this._cacheability; } } 

    private HttpCacheability _cacheability;

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
        cache.SetCacheability(_cacheability);
    }
}

接下来,一个动作:

Next, an action:

    [CacheControl(HttpCacheability.NoCache), HttpGet]
    public JsonResult MyAction()

这篇关于如何停止MVC缓存调用的操作方法的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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