Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http Redirect 302

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?

like image 378
DarthVader Avatar asked Aug 29 '11 17:08

DarthVader


2 Answers

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

like image 106
Yahia Avatar answered Nov 09 '22 05:11

Yahia


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.

like image 43
Chris Diver Avatar answered Nov 09 '22 04:11

Chris Diver