簡體   English   中英

緩存控制C#ASP.NET

[英]Cache Control C# ASP.NET

我在.net頁面上有一個菜單,在您第一次訪問該頁面時單擊該鏈接時會定期返回404。 我正在嘗試將頁面上的緩存設置為0,以消除服務器緩存作為錯誤的可能性。 有沒有辦法在HTML中設置它? 我正在尋找Response.Expires = -1作為頁面指令或類似的東西?

您可以定義這樣的過濾器

[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]
public class ControllerBase : Controller, IControllerBase

您還可以定義特定的控制器操作:

[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