http - ASP.NET MVC: Make browser cache images from action -



http - ASP.NET MVC: Make browser cache images from action -

i have actionmethod returns file , has 1 argument (an id).

e.g.

public actionresult icon(long id) { homecoming file(server.mappath("~/content/images/image" + id + ".png"), "image/png"); }

i want browser automatically cache image first time access next time doesn't have download data.

i have tried using things outputcacheattribute , manually setting headers on response. i.e:

[outputcache(duration = 360000)]

or

response.cache.setcacheability(httpcacheability.public); response.cache.setexpires(cache.noabsoluteexpiration);

but image still loaded every time nail f5 on browser (i'm trying on chrome , ie). (i know loaded every time because if alter image changes in browser).

i see http response has headers apparently should work:

cache-control:public, max-age=360000 content-length:39317 content-type:image/png date:tue, 31 jan 2012 23:20:57 gmt expires:sun, 05 feb 2012 03:20:56 gmt last-modified:tue, 31 jan 2012 23:20:56 gmt

but request headers have this:

pragma:no-cache

any thought on how this?

thanks lot

first thing note when nail f5 (refresh) in chrome, safari or ie images requested again, if they've been cached in browser.

to tell browser doesn't need download image 1 time again you'll need homecoming 304 response no content, per below.

response.statuscode = 304; response.statusdescription = "not modified"; response.addheader("content-length", "0");

you'll want check if-modified-since request header before returning 304 response though. you'll need check if-modified-since date against modified date of image resource (whether file or stored in database, etc). if file hasn't changed homecoming 304, otherwise homecoming image (resource).

here examples of implementing functionality (these httphandler same principles can applied mvc action method)

make browser cache output of httphandler http handler implementing dynamic resource caching

asp.net-mvc http caching

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -