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

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

问题描述

我在 IE 缓存操作方法的结果时遇到问题.

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

我发现的其他文章与安全性和 [Authorize] 属性有关.这个问题与安全无关.

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 运行它并使用 Firebug 观察 XHR 流量时,一切正常.然而,在 IE 8 下,throbber"图形永远没有时间显示,并且显示新"平均值和计数的页面元素从未不同.

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.

这篇文章似乎解决了这个问题,但我看不懂:防止在 ASP 中缓存属性.NET MVC,每次执行动作时强制执行属性

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 缺乏深入了解的人交谈一样,即使这意味着回复一篇关于所需基础知识/先决条件的文章.

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

接下来,一个动作:

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

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

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