2011-07-11 149 views
0

我有一個操作方法,我需要在點擊後退按鈕時執行。我之前通過禁用我的操作方法(Response.Cache.SetCacheability(HttpCacheability.NoCache))中的緩存來完成此操作。這不適用於其他操作方法。由於某些原因,當我禁用緩存並按下後退按鈕時?觸發我的操作方法在頁面過期什麼的問題可能是任何想法MVC後退按鈕問題

回答

1

有沒有辦法知道,在服務器端,如果頁面請求是後退按鈕的結果

更可能的是,以前的請求是一個帖子,而不是一個get,並且該帖子要求你重新發布數據。

+0

這確實是一個帖子。謝謝! – Quadwwchs

7

嘗試以下,對我的偉大工程:

public class NoCacheAttribute : ActionFilterAttribute 
{ 
    public override void OnResultExecuting(ResultExecutingContext filterContext) 
    { 
     var response = filterContext.HttpContext.Response; 
     response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); 
     response.Cache.SetValidUntilExpires(false); 
     response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); 
     response.Cache.SetCacheability(HttpCacheability.NoCache); 
     response.Cache.SetNoStore(); 
    } 
} 

public class HomeController : Controller 
{ 
    [NoCache] 
    public ActionResult Index() 
    { 
     // When we went to Foo and hit the Back button this action will be executed 
     // If you remove the [NoCache] attribute this will no longer be the case 
     return Content(@"<a href=""/home/foo"">Go to foo</a><div>" + DateTime.Now.ToLongTimeString() + @"</div>", "text/html"); 
    } 

    public ActionResult Foo() 
    { 
     return Content(@"<a href=""/home/index"">Go back to index</a>", "text/html"); 
    } 
} 
+0

看起來很有意思。謝謝! – Quadwwchs