Http Redirect 302

DarthVader picture DarthVader · Aug 29, 2011 · Viewed 13.9k times · Source

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?

Answer

Yahia picture Yahia · Aug 29, 2011

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