如何阻止MVC缓存调用动作方法的结果?

前端之家收集整理的这篇文章主要介绍了如何阻止MVC缓存调用动作方法的结果?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到了IE缓存操作方法结果的问题.

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

这是一个非常简单的“记录投票,抢平均值,返回平均值和投票数”的方法.关于它的唯一有趣的事情是它通过Ajax调用并返回一个Json对象.我相信它是被缓存的Json对象.

当我从FireFox运行它并使用Firebug观察XHR流量时,一切都运行良好.但是,在IE 8下,“throbber”图形没有时间显示,显示使用jQuery注入页面的“新”平均值和计数的页面元素永远不会有所不同.

我需要一种方法告诉M​​VC永远不要缓存这个动作方法.

这篇文章似乎解决了这个问题,但我无法理解它:
Prevent Caching of Attributes in ASP.NET MVC,force Attribute Execution every time an Action is Executed

我需要更多的上下文来解决方案,以了解如何扩展AuthorizationAttribute.请解决您的答案,就好像您正在与对MVC缺乏深入了解的人交谈,即使这意味着回复了一篇关于某些必要基础/先决条件的文章.

谢谢,

特雷卡罗尔

解决方法

MVC不会缓存结果. IE确实如此.

所以你必须告诉IE不要这样做.

这是我如何做到的.首先,属性

[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()
原文链接:https://www.f2er.com/aspnet/247073.html

猜你在找的asp.Net相关文章