C# 避免从缓存加载视图
如何避免使用缓存加载视图 我尝试将C# 避免从缓存加载视图,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,如何避免使用缓存加载视图 我尝试将[OutputCache(Duration=1)]放在通过ActionResult返回视图的方法之前,但没有成功 [OutputCache(Duration = 1)] public ActionResult Receita(string id, string periodo) { // Do my stuff var receita = new ReceitaAssunto() {
[OutputCache(Duration=1)]
放在通过ActionResult
返回视图的方法之前,但没有成功
[OutputCache(Duration = 1)]
public ActionResult Receita(string id, string periodo)
{
// Do my stuff
var receita = new ReceitaAssunto()
{
// More stuff
};
return View(receita);
}
当我通过方法的参数传递一个新值时,应该在我的视图中显示这个值,但它没有刷新,总是显示旧值
查看
@model Dashboard.Domain.ClasseTipada.ReceitaAssunto
<ul class="list-group clear-list m-t">
@{
var i = 1;
foreach(var elemento in Model.ReceitaPorTipoReceita)
{
<li class="list-group-item fist-item">
<span class="pull-right">
<span id="txtTextoValorLocacao">@elemento.Valor.ToString("C", new System.Globalization.CultureInfo("pt-BR"))</span>
</span>
<span class="label label-success">@i</span>@elemento.DescricaoOrigem
</li>
i++;
}
i = 0;
}
</ul>
将其放入
应用程序中的Global.asax.cs\u BeginRequest()。
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
它不起作用,我仔细检查了它通过receita对象的内容,没错,使用新的值。您使用的浏览器是哪一个/。我使用的是Firefox 32.0.1okk,因为您的数据已经被浏览器缓存了,它正在显示缓存的数据…只需清除您的缓存,您就不会再次遇到此问题…还需要重新清理和重建解决方案。清除它,清理浏览器缓存,重建解决方案,但是仍然是一样的。你能显示你的ajax调用吗?你是在尝试完全禁用缓存以执行
Receita
操作,还是只希望缓存副本随参数变化?@haim770更好的解释是:我有4个按钮(每月、每季度、每半年、每年)。当我单击每个按钮时,将通过参数向我的方法传递一个新值。所以,我的Receita视图不应该缓存这些值。你基本上需要缓存的副本随参数变化?是的,但我在这里做了一些测试,我通过JS得到了我想要的,但这很奇怪,因为在我看来,我有foreach,它工作正常,只是不在屏幕上呈现新值
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();