Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add caching for images used in my ASP.NET MVC web app?

I have read this http://madskristensen.net/post/add-expires-header-for-images and https://technet.microsoft.com/en-us/library/cc770661.aspx and other similar articles, who suggest to put this

<staticContent>
 <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
</staticContent>

But even after that the images are not fetched from cache and 200 ok response is sent, that is a request to the server is made. I want no request to be made for x hours/days coz these image wont change for a long time.

How do I do this ?

like image 761
Yasser Shaikh Avatar asked Jan 12 '16 08:01

Yasser Shaikh


People also ask

What is the correct way to apply caching in MVC?

In ASP.NET MVC, there is an OutputCache filter attribute that you can apply and this is the same concept as output caching in web forms. The output cache enables you to cache the content returned by a controller action. Output caching basically allows you to store the output of a particular controller in the memory.

What are the different caching techniques available in .NET MVC?

Any (Default)- Content is cached in three locations- the Web Server, any proxy Servers and the Web Browser. Client- Content is cached on the Web Browser. Server- Content is cached on the Web Server. ServerAndClient- Content is cached on the Web Server and the Web Browser.

What is cache in MVC?

The OutputCache filter allow you to cache the data that is output of an action method. By default, this attribute filter cache the data till 60 seconds. After 60 sec, if a call was made to this action then ASP.NET MVC will cache the output again.

Which control is used to add some dynamic content on the page when page caching is done?

Use the Substitution control to specify a section on an output-cached Web page where you want dynamic content substituted for the control. The Substitution control offers a simplified solution to partial page caching for pages where the majority of the content is cached.


1 Answers

The following configuration should cause browsers to cache your images for an entire year:

<staticContent>
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
<httpProtocol>
    <customHeaders>
        <add name="Cache-Control" value="public" />
    </customHeaders>
</httpProtocol>

You just need to make sure your images are being served as static file types. There is no way of forcing a browser to not send a request to the server ie; the user performs a hard fresh.

You can wrap the above configuration in a location node so as to only affect images that site in a certain path:

   <location path="Content">
            <system.webServer>
              <staticContent>
                <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
              </staticContent>
              <httpProtocol>
                <customHeaders>
                    <add name="Cache-Control" value="public" />
                </customHeaders>
              </httpProtocol>
            </system.webServer>
    </location>

The above configuration would add an HTTP cache header directive for all images hosted at http://www.example.com/Content/Images/*

You should create a configurable appsetting that is passed in to such images URI's as a query string parameter. This will allow you to clear all the clients to send a request the images from your server: (We want control over this as cached images can be problematic)

<img src="/Content/Images/MyImage.jpg?version=<%# Global.ImageVersion %>" />

More on caching headers (Cache-Control) here http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/

I hope that helps!

like image 121
Jean-Michel Gaud Avatar answered Nov 15 '22 06:11

Jean-Michel Gaud