I m trying to make a HTTP 302 Redirect, but getting the following exception while I'm running in debug mode.
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack
var response = HttpContext.Current.Response;
response.Clear();
response.Status = "302 Found";
response.AddHeader("Location", "http://google.com");
response.End();
response.Flush();
Long story short, this call is not flushing the response and not redirecting.
How can I get this working?
You shouldn't be calling both End
and Flush
in this way - for redirecting with HTTP 302 you should use HttpContext.Current.Response.Redirect
see http://msdn.microsoft.com/en-us/library/a8wa7sdt.aspx
The HttpResponse
object has a method for performing a 302 redirect.
Response.Redirect("page.aspx")
Although your code should work fine as that is a common way to implement a 301 redirect
.
Note that response.Flush()
is redundant as the response buffer is flushed to the client and execution will end on response.End()
, so that line will not be executed.
A google search for others with similar problems points to this KB article http://support.microsoft.com/kb/312629/EN-US/ which is likely to be the cause of your problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With