views:

158

answers:

2

I've noticed that when I Response.Redirect to an .aspx page, it sometimes displays a cached page instead of actually executing the page and pulling fresh data from the database.

I'm not using output caching or anything special in .Net here -- this is a CRM, and the caching is either happening on the client or, perhaps more likely, automatically in IIS.

There is never a querystring involved, by the way. I'm passing a key via session. I know that if I used the querystring it would probably partially bypass the cache problem, but it's not an option in this case, and anyway what I really want is a deeper understanding of what's going on.

I did a little digging, and some people get around this by using Server.Transfer (which actually behaves differently than Response.Redirect and some of the details are not always desirable), and some other people said to set Response.Cache.SetCacheability(HttpCacheability.NoCache) on the page where I want to avoid caching.

I would like a better understanding of what's going on here, and possibly a best practice -- for one thing, I thought .aspx pages were always flagged to avoid caching. Right?

Any thoughts?

+3  A: 

Response.Redirect sends a response to the browser that tells it the object requested has moved and provides it with the new location. In this case, if you redirect to "redirect.aspx", if the browser has that item in the cache, it will show it from there provided the cachability criteria are met.

You mention Response.Transfer, but you mean Server.Transfer. That is a server side mechanism to transfer execution context to a new page. So, the user requests "main.aspx" in your app. In that page, you Server.Transfer to "transferred.aspx". On the server side, it is the same request, so when the output is rendered, the client's browser will show a URL of "main.aspx", not "transferred.aspx".

To summarize - Redirect = 2 requests, Transfer = 1 request. Hope that helps.

And yes, you need to set cachability if you want the pages to always make a call to the server. By default, aspx has no special browser behavior, since it just sends html. You can set:

Response.Expires = -1 for example.

Nebakanezer
+1  A: 

By default no headers designed to control caching are sent. This leaves the client to make up its own rules about how to cache the content sent.

So yes you would need something like:-

Response.Cache.SetCacheability(HttpCacheability.NoCache) 

to ensure that a request (redirect or otherwise) does not simply use a cached version.

AnthonyWJones
@Brian: I would if if I were handling button clicks server-side.
AnthonyWJones