While performing a response.redirect in an ASP.NET page, I received the error:
error: cannot obtain value
for two of the variables being passed in (one value being retrieved from the querystring, the other from viewstate)
I've never seen this error before, so I did some investigating and found recommendations to use the "False" value for "endResponse"
e.g. Response.Redirect("mypage.aspx", False)
This worked.
My question is: what are the side-effects of using "False" for the "endResponse" value in a response.redirect?
i.e. are there any effects on the server's cache? Does the page remain resident in memory for a period? Will it affect different users viewing the same page? etc.
Thanks!
-
See also [is-response-end-considered-harmful](http://stackoverflow.com/questions/1087777/is-response-end-considered-harmful) – Michael Freidgeim May 30 '13 at 21:38
3 Answers
From this other question and this blog post, the recommended pattern is-
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();
This avoids the expensive ThreadAbortException/Response.End. Some code will be executed after the CompleteRequest()
, but ASP.Net will close the request as soon as it is convenient.
Edit- I think this answer gives the best overview. Note that if you use the pattern above, code after the redirect will still be executed.
-
-
Keep in mind that, since the request will be completed, many unnecessary KB of HTML may still be sent to the user. Depending on your code, they'll receive all the HTML for that page, which they'll never see, then all the HTML for the next page they're being redirected to. – Doug S Dec 06 '12 at 04:39
-
I've just started converting my old ASP.NET 2.0 project to MVC 5 with support for web forms, and there're 100s of pages where Response.Redirect("url") is used, is there any way to set endResponse to false globally, instead of modifying all the ages? – Mox Shah Nov 10 '14 at 05:43
-
1@MokshShah - I don't think so. In your case, I'd think about doing a global search and replace of Response.Redirect to a MySafeRedirect function, and setting end response there. – Spongeboy Nov 10 '14 at 22:34
-
An MSDN blog post that might answer your question:
The drawback to using [Response.Redirect(url, false)] is that the page will continue to process on the server and be sent to the client. If you are doing a redirect in Page_Init (or like) and call Response.Redirect(url, false) the page will only redirect once the current page is done executing. This means that any server side processing you are performing on that page WILL get executed.

- 4,093
- 3
- 31
- 29

- 44,706
- 7
- 102
- 124
Response.Redirect(..., true) results in a ThreadAbortException. depending on your exception handling setup you might get your log filled with error messages one for each redirect.

- 3,453
- 5
- 28
- 33