12
我使用ASP.NET的WebAPI,並有下面的代碼在一切停止緩存:如何停止Chrome緩存來自WebApi的REST響應?
public override System.Threading.Tasks.Task<HttpResponseMessage> ExecuteAsync(System.Web.Http.Controllers.HttpControllerContext controllerContext, System.Threading.CancellationToken cancellationToken)
{
System.Threading.Tasks.Task<HttpResponseMessage> task = base.ExecuteAsync(controllerContext, cancellationToken);
task.GetAwaiter().OnCompleted(() =>
{
task.Result.Headers.CacheControl = new CacheControlHeaderValue()
{
NoCache = true,
NoStore = true,
MaxAge = new TimeSpan(0),
MustRevalidate = true
};
task.Result.Headers.Pragma.Add(new NameValueHeaderValue("no-cache"));
task.Result.Content.Headers.Expires = DateTimeOffset.MinValue;
});
return task;
}
結果標題是這樣的(鉻):
Cache-Control:no-store, must-revalidate, no-cache, max-age=0
Content-Length:1891
Content-Type:application/json; charset=utf-8
Date:Fri, 19 Jul 2013 20:40:23 GMT
Expires:Mon, 01 Jan 0001 00:00:00 GMT
Pragma:no-cache
Server:Microsoft-IIS/8.0
我添加了「NO-存儲「後閱讀有關錯誤(How to stop chrome from caching)。
Request Method:GET
Status Code:200 OK (from cache)
沒有人有任何:
但是,無論我做什麼,當我做一些導航我離開這個頁面,然後使用「返回」按鈕,鍍鉻總是從緩存加載想法爲什麼發生這種情況?我已經確認服務器從未被這個請求命中。
如果您設置了無存儲,則不要設置no-cache,must-revalidate,max-age和過期,因爲它們與無存儲相沖突。無存儲意味着該表示不能被存儲,並且其餘的說明解釋用於管理存儲的表示的規則。 –
以及我剛剛添加無商店,因爲鉻錯誤,忽略無緩存和過期(見http://stackoverflow.com/questions/4146813/how-to-stop-chrome-from-caching)...所以,我不知道你想讓我做什麼。 – automaton
不幸的是,不緩存並不意味着不緩存。這也不意味着Chrome無法使用緩存副本。這意味着在使用緩存副本之前,它必須先檢查服務器。如果服務器沒有返回正確的信息,chrome會高興地返回緩存的結果。 –