2012-04-19 129 views
0

我有一個視圖,使ajax調用一個操作方法。Asp.net MVC 3緩存

$.ajax({ 
url: url to action, 
type: 'GET', 
cache: false, 
dataType: 'html', 
      success: function(result) { 
       $("#divPatient").html(result); 
       $("#divPatient").show("blind", { }, 2000); 
       $("#loadingImage").hide(); 
       PrepPatientHtml(); 
      } 
     }); 

如您所見,action方法返回html。該網站被驅逐出一個SQL數據庫,該數據庫在更改時應該會影響該操作的輸出。我添加了NoCache動作篩選器

public class NoCache : 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); 
     } 
    } 

由於某些原因,當對支持視圖的數據庫進行更改時,緩存永不會失效。任何人有任何想法?視圖是相當簡單:

@using (Html.BeginForm("Save", "Home", FormMethod.Post, new { id = "frm" })) 
{ 
    <div style="padding: 10px;"> 
     <h1 id="questionsHeader">@Model.FullName (@Model.Dob)</h1> 
     @for (var i = 0; i < Model.Episodes.Count; i++) 
     { 
      @Html.EditorFor(model => model.Episodes[i]) 
     } 
    </div> 
    <div style="padding-top: 10px; border-top: solid 1px #666666; margin-top: 5px; text-align: right;"> 
     <input type="submit" id="btnSaveAnswers" value="Save Answers" />   
    </div> 
} 

回答

0

使用此批註爲您的操作:

[OutputCache(Duration = 0)] 
+0

試過......相同的結果......它就好像實體框架爲我緩存數據一樣。 – jsteve81 2012-04-20 12:21:45

0

我不相信這將是框架或你所謂的「實體框架」(可能是你想說別的)。

您是否調試過您的應用程序以查看該操作是否實際上正在執行?

如果是的話,你有最終產生相同結果的對象緩存嗎?

我的猜測是你的瀏覽器正在緩存結果。

你有沒有嘗試這個辦法:

response.setHeader("Pragma", "no-cache"); 
response.setHeader("Cache-Control", "no-cache"); 
response.setDateHeader("Expires", 0); 

如果它不能正常工作,請嘗試更改URL通過添加一個隨機參數,如:

+ '&uid=' + uniqueId() 

其中UNIQUEID()是獲取功能你在時間上的唯一字符串就像urlencoded時間的滴答數。

雖然這最後一個選項可能不是最好的,但它肯定是最簡單,最快速並且肯定會工作的。

+0

爲什麼匿名downvote? – 2012-09-14 23:02:54

+1

+1我沒有想到querystring中的唯一性 – 2012-12-05 19:39:57