I have a method for resizing images in my application. The method returns an ImageResult like it is shown below.
[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();
}
}
The caching filter works as it is shown below.
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);
}
Now I run the application in Chrome I see in firebug (after the F5 refresh) in my Network tab that the request is under the Img tab and is cached from the client
When I run the application in Firefox I see (after the F5 refresh) in my Network tab that the request is under the Others tab and is not cached.
The things I tried are:
1.Use the OutputCache for the caching (with both server and client side caching) of the Action and the result was the same
[OutputCache(Duration = 1000000, Location = OutputCacheLocation.Server)]
2.Change the contentType of the result from the method from binary/octet-stream to image/jpeg and though I saw the request under the Images tag is still wasn't cached.
Does anyone know what is happening? I should remind if it is not clear that the caching mechanism for a user that just goes from one page to another (where the same request is used) works correctly. The only case it doesn't work is when the user refreshes the page by F5 or from the button (ctrl+R) in Firefox. (Since it is not a hard refresh this shouldn't happen)