Http Redirect 302

I am trying to make HTTP 302 Redirect, but I get the following exception while I am working 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(); 

In short, this call does not flush the answer, not redirect.

How can I make this work?

+6
source share
2 answers

You should not call both End and Flush this way - for redirection using HTTP 302 you should use HttpContext.Current.Response.Redirect see http://msdn.microsoft.com/en-us/library/a8wa7sdt.aspx

+7
source

The HttpResponse object has a method for redirecting 302.

 Response.Redirect("page.aspx") 

Although your code should work fine, as this is the usual way to implement 301 redirect .

Note that response.Flush() is redundant because the response buffer is flushed by the client and execution completes with response.End() , so the line will not be executed.

Finding other users using similar problems points to this article on KB http://support.microsoft.com/kb/312629/EN-US/ , which is likely to cause your problems.

+4
source

Source: https://habr.com/ru/post/896145/


All Articles