views:

55

answers:

1

I have an asp.NET application which has a fairly heavy mainpage. The content of this mainpage is only rarely changed, except by actions on that same page.

How do I make the browser retrieve the page from cache when the user presses the "Back" button in the browser, instead of contacting the server every single time, forcing it to render the page again?

Thanks, Jonas

+1  A: 

The short answer is: You can't.

I think the closest you can do is to include some HTML Meta tags in your webpage, specifically the one relating to caching and expiration such as:

<META HTTP-EQUIV="EXPIRES" CONTENT=" [some date/time in the future] ">

and

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="PUBLIC">

Once you've included these in your rendered web page that is sent to the client browser, you just have to hope that the browser, upon the user clicking the back button, will displayed a "cached" version of your page rather than instigating another request/response cycle to your web server, thereby causing your web page to be re-requested and re-rendered. You will have no control over this, though, and it's entirely down to how the browser wants to handle it. I believe most modern browsers will re-request the page, though.

Another approach to preventing re-rendering of an ASP.NET page is to control this yourself on the server-side using the

@OutputCache

directive within your ASP.NET web page, like this:

<%@ OutputCache Duration="600" VaryByParam="None" %>

At it's most basic, the @OutputCache control will cache your rendered ASP.NET page for a specified number of seconds (600 in the example above) so that subsequent requests for that page will result in the ASP.NET runtime serving up the already rendered version of the page to the user, rather than re-processing and re-rendering the entire page. Be aware that this is not user-specific, though.

More info on page output caching (which is probably the closest you'll get to what you're after):

Page Output Caching
The OutputCache

CraigTP
So using the OutputCache directive will work, as long as all arguments that differentiate the pages are included in the URL. I'll give that a try, thanks :)
Joda
@Joda - Doesn't necessarily have to be in the URL. If it's a "GET" request, that'll be in the URL via the querystrings, however, the "VaryByParam" attribute on the @OutputCache directive will also work for "POST" requests too. You can also cache only parts of an ASP.NET web page with "donut caching". See here: http://msdn.microsoft.com/en-us/library/h30h475z.aspx
CraigTP
Sadly, it doesn't allow me to solve my problem without substantial changes to the code - most of what I'd need to differentiate generated pages is saved in session. However, it answered my question, so I thank you for that :)
Joda