Search code examples
asp.net-mvcdonut-caching

DonutOutputCache not working for an image


I have an ASP.NET MVC controller action like the following:

[HttpGet]
[DonutOutputCache(CacheProfile = "banner")] 
public async Task<ActionResult> Banner(string name, string size = "lg")
{
    // snipped - work out which banner to show today.

    return File(thePathToSomeBannerImageFile, "image/jpeg");
}

And the configuration file looks like:

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      ...
      <add name="banner" duration="31536000" varyByParam="*" />
      ...
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

Since adding DonutOutputCache the image that I used to render to my browser now won't load. I can see in the Fiddler trace that a largish binary file is getting to the browser, but I can't seem to get it to render.

Removing the DonutOutputCache makes the image appear again.

I've tried clearing the cache etc. Any ideas?


Solution

  • Ah, just figured it out from this link: Doesn't work w/ Files?.

    Basically DonutOutputCache uses ContentResult internally which works only with text-based content. Since I'm returning a binary response in a FileResult it messes stuff up. Fortunately it works side-by-side with the built in OutputCache, so I could modify my action as follows and it works as expected. I really only needed DonutOutputCache for other bits of my site that are text-based. I tripped myself up trying to just have one caching attribute to rule them all.

    [HttpGet]
    [OutputCache(CacheProfile = "banner")] 
    public async Task<ActionResult> Banner(string name, string size = "lg")
    {
    
    }