我有一个ASP。NET MVC 3应用程序。这个应用程序通过jQuery请求记录。jQuery回调控制器动作,返回JSON格式的结果。我还不能证明这一点,但我担心我的数据可能会被缓存。

我只希望缓存应用于特定的操作,而不是所有的操作。

是否有一个属性可以放在操作上以确保数据不会被缓存?如果不是,我如何确保浏览器每次都获得一组新的记录,而不是缓存的记录集?


当前回答

为了确保JQuery不会缓存结果,在你的ajax方法中,放入以下内容:

$.ajax({
    cache: false
    //rest of your ajax setup
});

或者为了防止在MVC中缓存,我们创建了自己的属性,你也可以这样做。下面是我们的代码:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext 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();

        base.OnResultExecuting(filterContext);
    }
}

然后用[NoCache]装饰你的控制器。或者,你可以把属性放在你继承控制器的基类的类上(如果你有一个),就像我们这里:

[NoCache]
public class ControllerBase : Controller, IControllerBase

如果你需要一些动作是不可缓存的,你也可以用这个属性来装饰它们,而不是装饰整个控制器。

如果你的类或动作在浏览器中呈现时没有NoCache,而你想检查它是否正常工作,请记住在编译更改后,你需要在浏览器中进行“硬刷新”(Ctrl+F5)。在你这样做之前,你的浏览器会保留旧的缓存版本,不会用“正常刷新”(F5)来刷新它。

其他回答

ASP。NET MVC 5解决方案:

在中央位置缓存防止代码:App_Start/FilterConfig.cs的RegisterGlobalFilters方法:

    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            // ...
            filters.Add(new OutputCacheAttribute
            {
                NoStore = true,
                Duration = 0,
                VaryByParam = "*",
                Location = System.Web.UI.OutputCacheLocation.None
            });
        }
    }

一旦你有了它,我的理解是,你可以通过在控制器或视图级别应用不同的OutputCache指令来覆盖全局过滤器。对于常规控制器来说

[OutputCache(NoStore = true, Duration = 0, Location=System.Web.UI.ResponseCacheLocation.None, VaryByParam = "*")]

或者如果它是ApiController,它就是

[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, Location = System.Web.UI.OutputCacheLocation.None, VaryByParam = "*")]

在控制器动作中,将以下行附加到标题

    public ActionResult Create(string PositionID)
    {
        Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
        Response.AppendHeader("Expires", "0"); // Proxies.

你只需要:

[OutputCache(Duration=0)]
public JsonResult MyAction(

或者,如果你想为整个控制器禁用它:

[OutputCache(Duration=0)]
public class MyController

尽管在这里的评论中有争论,但这足以禁用浏览器缓存——这会导致ASP。Net发出响应头,告诉浏览器文档立即过期:

为了确保JQuery不会缓存结果,在你的ajax方法中,放入以下内容:

$.ajax({
    cache: false
    //rest of your ajax setup
});

或者为了防止在MVC中缓存,我们创建了自己的属性,你也可以这样做。下面是我们的代码:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext 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();

        base.OnResultExecuting(filterContext);
    }
}

然后用[NoCache]装饰你的控制器。或者,你可以把属性放在你继承控制器的基类的类上(如果你有一个),就像我们这里:

[NoCache]
public class ControllerBase : Controller, IControllerBase

如果你需要一些动作是不可缓存的,你也可以用这个属性来装饰它们,而不是装饰整个控制器。

如果你的类或动作在浏览器中呈现时没有NoCache,而你想检查它是否正常工作,请记住在编译更改后,你需要在浏览器中进行“硬刷新”(Ctrl+F5)。在你这样做之前,你的浏览器会保留旧的缓存版本,不会用“正常刷新”(F5)来刷新它。

MVC中的输出缓存

[OutputCache(NoStore = true, Duration = 0, Location="None", VaryByParam = "*")]

OR
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]