[英]mvc editor templates result is cached
我將編輯器模板與自定義母版頁一起使用
Html.EditorFor(o => o.Name)
生成標簽和輸入,我還使用自定義DisplayName屬性來本地化標簽
[DisplayNameLocalized("Name")]
public string Name {get;set;}
我在屬性的構造函數中放了一個斷點,並注意到它只在我第一次使用EditorFor渲染頁面時被調用,所以我猜編輯器的結果是緩存的,有人知道如何避免這種緩存嗎?
理想情況下,您需要在Action上使用[NoCache]
屬性。
public 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);
}
}
也可以在模板中使用<%@ OutputCache %>
指令 - 但有些人抱怨它並不總是有效。
供參考,請看這里 。
您可以使用<%@ OutputCache NoStore="true" %>
我遇到過同樣的問題。 這適合我
public ActionResult Index(int? pageNumber)
{
var wishlistModel = new WishlistModel();
BindGifts(wishlistModel, pageNumber);
if (Request.IsAjaxRequest())
{
ViewData.ModelState.Clear();
return PartialView("_UserGiftList", wishlistModel);
}
return View(wishlistModel);
}
在對MVC源代碼進行一些挖掘之后,我發現所有Html幫助程序都從ViewData.ModelState對象獲取數據,並且在ajax Request之后緩存了ModelState緩存的未知原因。
部署到Azure網站時,我遇到了類似的問題。 一直在展示舊版的EditorTemplate。 我嘗試手動發布cshtml文件,我嘗試FTPing到網站並刪除Views文件夾和Bin文件夾; 但仍然是這個編輯模板的幽靈正在困擾着這個網站!
最終工作的是檢查“ 構建”>“發布”>“設置”>“文件發布選項”下的“ 刪除目的地中的其他文件 ”選項 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.