使用属性防止在 ASP.NET MVC 中缓存特定操作 [英] Prevent Caching in ASP.NET MVC for specific actions using an attribute

查看:21
本文介绍了使用属性防止在 ASP.NET MVC 中缓存特定操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an ASP.NET MVC 3 application. This application requests records through jQuery. jQuery calls back to a controller action that returns results in JSON format. I have not been able to prove this, but I'm concerned that my data may be getting cached.

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

I only want the caching to be applied to specific actions, not for all actions.

是否有一个属性可以让我执行操作以确保数据不会被缓存?如果没有,我如何确保浏览器每次获得一组新记录,而不是缓存组?

Is there an attribute that I can put on an action to ensure that the data does not get cached? If not, how do I ensure that the browser gets a new set of records each time, instead of a cached set?

推荐答案

为确保 JQuery 不会缓存结果,请在您的 ajax 方法中添加以下内容:

To ensure that JQuery isn't caching the results, on your ajax methods, put the following:

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

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

Or to prevent caching in MVC, we created our own attribute, you could do the same. Here's our code:

[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] 装饰你的控制器.或者要做到这一点,您只需将属性放在您继承控制器的基类的类上(如果您有控制器),就像我们在这里一样:

Then just decorate your controller with [NoCache]. OR to do it for all you could just put the attribute on the class of the base class that you inherit your controllers from (if you have one) like we have here:

[NoCache]
public class ControllerBase : Controller, IControllerBase

如果您需要不可缓存的操作,您还可以使用此属性装饰一些操作,而不是装饰整个控制器.

You can also decorate some of the actions with this attribute if you need them to be non-cacheable, instead of decorating the whole controller.

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

If your class or action didn't have NoCache when it was rendered in your browser and you want to check it's working, remember that after compiling the changes you need to do a "hard refresh" (Ctrl+F5) in your browser. Until you do so, your browser will keep the old cached version, and won't refresh it with a "normal refresh" (F5).

这篇关于使用属性防止在 ASP.NET MVC 中缓存特定操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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