3

I have an ASP.NET MVC3 application setup. There is a controller that returns back images and I have added the following:

[OutputCache(Duration = 3600, VaryByParam = "id;width", Order = 1000, Location = OutputCacheLocation.Client)]
public ActionResult Get(string id, int width)
{ ... }

But when I check out the HTTP Response on these images they all have headers that say "cache-control: no-cache" and "expires: -1" which means the browser is never caching them.

I'm looking all around and I can't find anything on why the response is telling the browser not to cache them. I even tried working up my own attribute that did:

public class ContentExpiresHeader : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        var cache = filterContext.HttpContext.Response.Cache;
        cache.SetExpires(DateTime.Now.AddYears(1));
        cache.SetCacheability(HttpCacheability.Private);
        cache.SetLastModifiedFromFileDependencies();
        base.OnResultExecuted(filterContext);
    }
}

but that didn't get me anywhere either.

Any help is appreciated.

UPDATE: I'm starting to think this has got to be an IIS setting somewhere that is adding the no-cache and overriding. I can't seem to find anything, though. The only odd thing is that if I take a look at the state of the cache variable after I've called the .Set...() methods the internal variables have not been updated. I would have expected something to change but they're still showing "no-cache".

UPDATE 2: I should add that the return of this method is a:

return File(...);

UPDATE 3: I also found this (http://dotnetslackers.com/articles/aspnet/ASP-NET-MVC-3-Controller-for-Serving-Images.aspx) and tried implementing it without any luck. Still getting the no-cache options on the response header for the images.

UPDATE 4: Just had to check server settings... if I bypass my controller and go straight to an image file on the server, then it DOES cache and has the correct caching settings in the response header.

UPDATE 5 (yeah, getting crazy): Created a brand new MVC3 project and just made the one controller and it cached just fine. So I've got something outside the immediate code that is adding this pragma:no-cache stuff and for the life of me I can't figure out what it'd be. =-/

4
  • Is this happenning on all browsers? Try cleaning the cache and then check again, I had a similar problem when using compression
    – pollirrata
    Jul 13, 2012 at 3:39
  • Clearing cache and trying different browsers results in the same, the response headers say "no-cache". Jul 13, 2012 at 12:43
  • just an fyi, this can happen in IE over ssl: stackoverflow.com/questions/9217823/… although you do say multiple browsers, so do you have a global action filter assigning a different cache policy? Jul 13, 2012 at 18:42
  • @AdamTuliper I've reviewed all my global filters and nothing appears to be overriding anything. I'm testing/dev in Chrome. Jul 13, 2012 at 18:44

2 Answers 2

0

Try changing the cacheability from HttpCachability.Private to HttpCachability.ServerAndPrivate. It should keep the cache-control as private and not suppress e-tags/last modified.

1
  • No luck, response headers are still "no-cache". Jul 13, 2012 at 12:43
0

Found the problem! And it's the weirdest thing I've seen in a while.

I am using SocialAuth-net and somewhere during the setup I added the system.webServer module for it and set runAllManagedModulesForAllRequests=true. I thought, "huh, wonder if that's causing it somehow", as I couldn't reproduce the problem outside this particular app. Low and behold, commenting out that section of the config and my images started caching! Hoorah!

But, it gets weirder. I undid my config changes, refreshed and now I'm still getting caching. I can't tell you how many resets of the system I've done with no change but somehow temporarily removing these modules from the pipeline seems to have resolved this problem.

I can track it down to the SocialAuthHttpModule and if I remove it SocialAuth-net still seems to work but caching is restored reliably. Very weird.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.