2017-06-15 103 views
1

我在.net頁面上有一個菜單,當您第一次訪問該頁面時,該頁面會定期返回404鏈接。我試圖在頁面上設置緩存爲0,以消除服務器緩存作爲錯誤的可能性。有沒有辦法在HTML中設置它?我正在尋找Response.Expires = -1作爲頁面指令或沿着這些線?緩存控制C#ASP.NET

+0

是webforms還是mvc或web api或其他東西? – mjwills

+0

通常可以在F12開發人員工具中關閉瀏覽器緩存。 –

+0

@IainBallard我認爲他想改變服務器端,而不是要求每一位訪問者這樣做。 – Alejandro

回答

0

你可以這樣定義

[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)] 
0

將此添加到您的控制器中的Action方法中:

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