我遇到了浏览器页面缓存问题,特别是 IE 11,SomePage 页面始终从浏览器缓存加载。同一页面在 Edge、Chrome 和 FF 中完美运行。
该页面是从 JS 函数调用的,如下所示:
$.ajaxJsonAntiForgery({
type: "POST",
url: '@Html.Raw(Url.Action("SomeAPI", "cntrl"))',
dataType: "json",
data: { model: model },
success: function (result) {
if (result.Success) {
window.location = '@Html.Raw(Url.Action("SomePage", "cntrl"))';
...
我尝试过以下方法:
在 URL 中附加一些随机数。 - 没用
在操作方法中添加了以下代码:- 不起作用
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore(); Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); Response.Cache.SetMaxAge(TimeSpan.Zero); Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
在 Fiddler 中我可以看到以下标题:
在 SomePage 操作方法上添加 OutputCache 属性。 - 没用
[OutputCache(NoStore = true,持续时间 = 0,VaryByParam = "*",位置 = System.Web.UI.OutputCacheLocation.None)]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
请建议,此页面标题中还应该包含哪些内容,这将指示浏览器 - 它不应缓存此页面?
最佳答案
您可以全局关闭缓存。或者通过装饰 Controller 中的 Action
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
Public ActionResult SomeApi(string param){
...
}
In ASP.NET MVC, you can use the OutputCacheAttribute attribute to mark action methods whose output you want to cache. If you mark a controller with the OutputCacheAttribute attribute, the output of all action methods in the controller will be cached [or not cached depending on the attribute values]. MSDN Article
还值得注意的是:如果您的页面实际上没有更改,则 ASP.NET 足够智能,可以使用缓存页面来提高速度。
<小时/>尝试上述故障排除步骤后进行更新。 在通话之前设置您的 Ajax 怎么样?
$.ajaxSetup({ cache: false});
关于javascript - 使用 MVC 5 的 IE11 缓存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42569000/