原文 http://www.cnblogs.com/super-cj/archive/2012/11/07/2758472.html
讓網頁“立即過時”有時候是一個很有用的功能,比如防止使用者使用後退來破壞程式邏輯;程式主動重新整理某個頁面等等。在ASP中最有效方法是:
Response.CacheControl = "no-cache"
就這麼一句足矣,Response.Expires=-1之類的其實都不需要,也不符合我們的要求。
但到了ASP.Net的時代,當我還糊裡糊塗地使用這條語句時,發現MSDN中對Response的這個欄位的說明是:
Sets the Cache-Control HTTP header to Public or Private.
[C#]
public string CacheControl {get; set;} Property Value
"Public" or "Private".
Exceptions
ArgumentException: CacheControl is an invalid cache control value (not Private or Public).
怎麼只允許賦"public"和"private"兩種值了?但是一實驗方知是MS在唬人,只要是合法的cache-control的HTTP head值都可以接受,當然包括no-cache了。
而且經實踐,這種方法的功能仍然與ASP中是一樣的。當然,似乎.Net官方推薦這種方法:
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
這個效果和前者完全一致。
最後,有一點要注意,似乎只有IE的結果是對的,Firefox不買這個帳……
只要直接在頁面的page_load 中添加一句:
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
只要使用者後退就會提示頁面到期。