如何从 asp.Net mvc 应用程序禁用自动浏览器缓存?
因为我在缓存所有链接时遇到了问题。但有时它会自动重定向到默认索引页面,该页面将其存储在缓存中,然后在我一直点击该链接时,它会将我重定向到默认索引页面。
那么有人知道如何从 ASP.NET MVC 4 手动禁用缓存选项吗?
如何从 asp.Net mvc 应用程序禁用自动浏览器缓存?
因为我在缓存所有链接时遇到了问题。但有时它会自动重定向到默认索引页面,该页面将其存储在缓存中,然后在我一直点击该链接时,它会将我重定向到默认索引页面。
那么有人知道如何从 ASP.NET MVC 4 手动禁用缓存选项吗?
您可以使用OutputCacheAttribute
来控制服务器和/或浏览器对特定操作或控制器中所有操作的缓存。
禁用控制器中的所有操作
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will be applied to all actions in MyController, unless those actions override with their own decoration
public class MyController : Controller
{
// ...
}
禁用特定操作:
public class MyController : Controller
{
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)] // will disable caching for Index only
public ActionResult Index()
{
return View();
}
}
如果您想将默认缓存策略应用于所有控制器中的所有操作,您可以通过编辑并查找方法来添加全局操作过滤器。此方法添加在默认的 MVC 应用程序项目模板中。global.asax.cs
RegisterGlobalFilters
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new OutputCacheAttribute
{
VaryByParam = "*",
Duration = 0,
NoStore = true,
});
// the rest of your global filters here
}
这将导致它将上述OutputCacheAttribute
指定应用于每个操作,这将禁用服务器和浏览器缓存。您仍然应该能够通过添加OutputCacheAttribute
特定操作和控制器来覆盖此无缓存。
HackedByChinese 没有抓住重点。他把服务器缓存误认为是客户端缓存。OutputCacheAttribute 控制服务器缓存(IIS http.sys 缓存),而不是浏览器(客户端)缓存。
我给你我代码库的一小部分。明智地使用它。
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class NoCacheAttribute : FilterAttribute, IResultFilter
{
public void OnResultExecuting(ResultExecutingContext filterContext)
{
}
public void OnResultExecuted(ResultExecutedContext filterContext)
{
var cache = filterContext.HttpContext.Response.Cache;
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetRevalidation(HttpCacheRevalidation.ProxyCaches);
cache.SetExpires(DateTime.Now.AddYears(-5));
cache.AppendCacheExtension("private");
cache.AppendCacheExtension("no-cache=Set-Cookie");
cache.SetProxyMaxAge(TimeSpan.Zero);
}
}
用法:
/// will be applied to all actions in MyController
[NoCache]
public class MyController : Controller
{
// ...
}
明智地使用它,因为它确实禁用了所有客户端缓存。唯一未禁用的缓存是“后退按钮”浏览器缓存。但似乎真的没有办法绕过它。也许只能通过使用 javascript 来检测它并强制页面或页面区域刷新。
我们可以在 Web.config 文件中设置缓存配置文件,而不是在页面中单独设置缓存值,以避免冗余代码。我们可以使用 OutputCache 属性的 CacheProfile 属性来引用配置文件。此缓存配置文件将应用于所有页面,除非页面/方法覆盖这些设置。
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="CacheProfile" duration="60" varyByParam="*" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>
如果您想禁用特定操作或控制器的缓存,您可以通过装饰特定操作方法来覆盖配置缓存设置,如下所示:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult NoCachingRequired()
{
return PartialView("abcd");
}
希望这很清楚并且对您有用。
如果要防止浏览器缓存,可以使用ShareFunction 中的此代码
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);
}
对于页面解决方案,请在布局页面中设置:
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
为了让所有人都能看到我的答案,我将我的评论移至回答这个问题。
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
这适用于所有浏览器(IE、Firefox 和 Chrome)。很高兴听到我的回答对你有用@Joseph Katzman