javascript - 使用 MVC 5 的 IE11 缓存问题

标签 javascript asp.net-mvc caching

我遇到了浏览器页面缓存问题,特别是 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"))';
        ...

我尝试过以下方法:

  1. 在 URL 中附加一些随机数。 - 没用

  2. 在操作方法中添加了以下代码:- 不起作用

    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 中我可以看到以下标题:

Fiddler

  • 在 SomePage 操作方法上添加 OutputCache 属性。 - 没用

    [OutputCache(NoStore = true,持续时间 = 0,VaryByParam = "*",位置 = System.Web.UI.OutputCacheLocation.None)]

  • Fiddler

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    

    enter image description here

    请建议,此页面标题中还应该包含哪些内容,这将指示浏览器 - 它不应缓存此页面?

    最佳答案

    您可以全局关闭缓存。或者通过装饰 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/

    相关文章:

    javascript - 使用事件的feathersjs套接字

    javascript - 如何切换 JQuery .$post url

    javascript - 时间线操作布局 - 不显示附件

    asp.net - Windows身份验证下HttpContext.Current.User为null(集成管道)

    c# - 使用 IoC 解析 Action Methods 中的模型对象

    Java - HttpUrlConnection 每次都返回缓存的响应

    ruby-on-rails - 集合的低级缓存

    javascript - 如何从 SXSSFWorkbook 创建 PIVOT 表 - APACHE POI

    css - 在 mvc 3 razor 中将整数值显示到 img 标签中

    java - Java 级 DNS 缓存的目的是什么?