简体   繁体   English

缓存控制C#ASP.NET

[英]Cache Control C# ASP.NET

I have a menu on a .net page that periodically returns a 404 when clicking the link the first time you visit the page. 我在.net页面上有一个菜单,在您第一次访问该页面时单击该链接时会定期返回404。 I'm trying to set the caching to 0 on the page to eliminate server caching as a possibility for the error. 我正在尝试将页面上的缓存设置为0,以消除服务器缓存作为错误的可能性。 Is there a way to set it in the HTML? 有没有办法在HTML中设置它? I'm looking for Response.Expires = -1 as a page directive or something along those lines? 我正在寻找Response.Expires = -1作为页面指令或类似的东西?

You can define a filter like this 您可以定义这样的过滤器

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

And while defining a controller you can annotation: 在定义控制器时,您可以注释:

[NoCache]
public class ControllerBase : Controller, IControllerBase

Also you can define in particular controller action: 您还可以定义特定的控制器操作:

[OutputCache(NoStore = true, Duration = 0)]

将其添加到控制器内的Action方法中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM