Asp.net mvc 对于返回图像的ActionResults,服务器端和客户端缓存在Firefox上的F5上不起作用

Asp.net mvc 对于返回图像的ActionResults,服务器端和客户端缓存在Firefox上的F5上不起作用,asp.net-mvc,firefox,caching,actionfilterattribute,image-caching,Asp.net Mvc,Firefox,Caching,Actionfilterattribute,Image Caching,在我的应用程序中,我有一种调整图像大小的方法。该方法返回一个ImageResult,如下所示 [CachingActionFilter(_maxAgeDuration)] public ActionResult GetStaticImage(string filePath, int w, int h) { try { FileInfo fileInfo = new FileInfo(Server.MapPath(fi

在我的应用程序中,我有一种调整图像大小的方法。该方法返回一个ImageResult,如下所示

    [CachingActionFilter(_maxAgeDuration)]
    public ActionResult GetStaticImage(string filePath, int w, int h)
    {
        try
        {
            FileInfo fileInfo = new FileInfo(Server.MapPath(filePath));
            if (!fileInfo.Exists)
            {
                LogException($"Could not find {fileInfo.FullName}. GetStaticImage() returns HttpNotFoundResult");
                return new HttpNotFoundResult();
            }

            var img = new WebImage(fileInfo.FullName).Resize(w, h, false, true);
            return new ImageResult(new MemoryStream(img.GetBytes()), "binary/octet-stream");
        }
        catch (Exception ex)
        {
            LogException(ex);
            return new HttpNotFoundResult();
        }
    }
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var httpContext = filterContext.RequestContext.HttpContext;

        // Some validations not relative to the issue 

        httpContext.Response.Cache.SetCacheability(HttpCacheability.Private);
        httpContext.Response.Cache.SetMaxAge(new TimeSpan(0, 0, _maxAgeSeconds));

        base.OnResultExecuting(filterContext);
    }
缓存过滤器的工作方式如下所示

    [CachingActionFilter(_maxAgeDuration)]
    public ActionResult GetStaticImage(string filePath, int w, int h)
    {
        try
        {
            FileInfo fileInfo = new FileInfo(Server.MapPath(filePath));
            if (!fileInfo.Exists)
            {
                LogException($"Could not find {fileInfo.FullName}. GetStaticImage() returns HttpNotFoundResult");
                return new HttpNotFoundResult();
            }

            var img = new WebImage(fileInfo.FullName).Resize(w, h, false, true);
            return new ImageResult(new MemoryStream(img.GetBytes()), "binary/octet-stream");
        }
        catch (Exception ex)
        {
            LogException(ex);
            return new HttpNotFoundResult();
        }
    }
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var httpContext = filterContext.RequestContext.HttpContext;

        // Some validations not relative to the issue 

        httpContext.Response.Cache.SetCacheability(HttpCacheability.Private);
        httpContext.Response.Cache.SetMaxAge(new TimeSpan(0, 0, _maxAgeSeconds));

        base.OnResultExecuting(filterContext);
    }
现在,我在Chrome中运行应用程序,我在网络选项卡的firebug(F5刷新后)中看到,请求位于Img选项卡下,并从客户端缓存

当我在Firefox中运行应用程序时,我在我的网络选项卡中看到(在F5刷新之后),请求位于其他选项卡下,并且没有被缓存

我试过的是:

1.将OutputCache用于操作的缓存(同时使用服务器端和客户端缓存),结果是相同的

[OutputCache(Duration = 1000000, Location = OutputCacheLocation.Server)]
2.将方法中结果的contentType从binary/octet stream更改为image/jpeg,尽管我看到了Images标记下的请求,但仍然没有缓存


有人知道发生了什么吗?如果不清楚用户从一个页面转到另一个页面(使用相同的请求)的缓存机制是否正常工作,我应该提醒您。唯一不起作用的情况是当用户通过F5刷新页面或通过Firefox中的按钮(ctrl+R)刷新页面时。(因为这不是一个硬刷新,所以不应该发生)

这似乎是FireFox的一个实现选择。您的服务器可以添加它想要的所有缓存头,但是否注意它们取决于客户端的实现。顺便说一句,“刷新”的实现并不是web标准中规定的,因此,浏览器之间可能存在无法规范化的差异。我可能会认为,提到的ctrl+f5是硬刷新,并显式用于刷新浏览器中的缓存项,这表明简单刷新没有这样做。但从你的角度来看,它可能是浏览器不想缓存的东西。这些天来,我从这个角度所看到的一切并没有让我走到任何地方。有些人可能提到ImageResults的缓存方式与jsonResults不同,例如,尤其是服务器部分缓存不起作用,这对我来说是最奇怪的事情。您提到它“在用户从一个页面转到另一个页面时正常工作”。就web开发而言,这就是您所希望的,因为后退、前进和刷新按钮可以执行实现者决定的任何操作。无论如何,web应用程序依赖这些按钮而不是提供自己的导航是非常不寻常的。也就是说,在某些场景中禁用缓存以确保(例如)一系列已完成的表单不允许用户向后导航以查看敏感数据,这并不罕见。