3

I am using asp.net mvc and have a view that displays an item and a view that allows editing an item. When the user submits an edited item, it redirects them to the view for that item. The changes are not being reflected when they are redirected because the item view is being cached. The view item page can also be edited inline and then a submit button uses javascript to submit and then refresh the page.

I've noticed that stackoverflow somehow makes this work when you edit an item, it redirects you to the page with your changes reflected. Their caching is set to public, max-age=120 so I am surprised the browser doesn't pull a local copy without the reflected changes...

Anyway, is there a way to force a cache refresh in these cases? I would prefer not to append a random query string value, but if there is no other way that is what I will do.

jjxtra
  • 20,415
  • 16
  • 100
  • 140

1 Answers1

1

There is a cache-control meta tag you can set. Put this in your head section

<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">

http://www.chami.com/tips/internet/113096I.html

or the .Net way

<%@ OutputCache Location="None" VaryByParam="None" %>

http://www.dailycoding.com/Posts/how_not_to_cache_a_page_output_in_aspnet.aspx

Greg Randall
  • 811
  • 7
  • 14
  • 1
    Would this be on the view that is displaying the item? We definitely want that page cached if at all possible, the problem is if it's already cached and the user updates the item, it will still be cached. This solution requires disabling caching entirely, which is not an option for our view item page. – jjxtra Apr 12 '11 at 01:31
  • Sounds like you need to ensure your page is displaying the correct data, and not the old data. If you save your data. I'm guessing it still shows the old data on your screen. What happens if you press ctrl F5? – Greg Randall Apr 12 '11 at 02:26
  • ctrl-F5 does not refresh the data, it brings back the cached server-side copy. If I change my caching to private, ctrl-F5 does update to the latest data. – jjxtra Apr 12 '11 at 02:51
  • Control F5 forced the browser not to use the cache, so that shows you what data you are putting into your page. You could turn caching off using the suggested methods and see what is returned on your page. – Greg Randall Apr 13 '11 at 01:28
  • Can you explain more what you mean with the meta tags? I assume I don't want them unless the user is being redirected from an edit, but in that case the browser is just pulling the local cached copy. I am going to nuke the server side cache and then try doing a redirect to the view url with caching disabled and see if that works... – jjxtra Apr 13 '11 at 17:17
  • Yeah, my point was to not cache anything, and see your result. That way you can tell if its a caching error or an error in your code. – Greg Randall Apr 13 '11 at 20:26