將輸出緩存屬性添加到您的控制器或操作。我建議你在你的web.config中使用一個緩存配置文件來實現這一點。
[OutputCache(CacheProfile = "NoCache")]
public class MyController : Controller
{
}
這將在您的web.config中system.web元素下。
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<clear />
<add name="NoCache" varyByParam="None" location="ServerAndClient" noStore="true" duration="0" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
現在一件事是棘手的是與MVC的新版本(3,4),你會得到一個「出現InvalidOperationException:時長必須爲正數」,如果你對孩子的行動應用緩存配置文件(即如果你使用@ Html.RenderAction)。所以如果你的行爲被這樣調用,你將無法使用它的OutputCache屬性。而是在將會呈現子操作的父操作上使用OutputCache屬性。
實施例:
public class MyController : Controller
{
[OutputCache(CacheProfile="NoCache")]
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public ActionResult ChildAction()
{
return View();
}
}