[ACCEPTED]-Response.Redirect not ending execution-response.redirect

Accepted answer
Score: 34

The second parameter for Response.Redirect 13 is endResponse, however the tooltip says 'Indicates 12 whether execution of the current page should 11 terminate'. This is misleading, because 10 the execution of the page does not actually 9 terminate when the variable is true. It 8 will finish running any code. However what 7 does happen differently, is the Render events 6 are canceled, and the Response is immediately 5 flushed with the object moved header.

You 4 need to manually exit out of any methods, Response.Redirect 3 / Response.End will not do that for you. Futhermore, if 2 you need a conditional to see if the Page 1 has been redirected, check out Response.IsRequestBeingRedirected.

Score: 12

are you exiting from the function that calls 1 redirect, e.g.

...redirect(stopit,true);
return;

?

Score: 3

Probably you are calling the Response.Redirect 4 method inside a try{}catch{} block, try 3 it by calling outside of this block and 2 you'll see that it will not fail. More info: http://www.velocityreviews.com/forums/t72105-responseredirect-in-a-trycatch.html Hope 1 this helps.

Score: 3

You can throw an exception, that will exit 1 code execution, but still redirect:

HttpContext.Current.Response.Redirect("/login", true);
throw new Exception("Unauthorized Access");
Score: 1

I'm going to get down voted for this! Curse 11 my ignorance...

Obviously you could try adding 10 the following line after the redirect (as 9 pointed out by recursive),

response.end()

But maybe the 8 reason the response.redirect is not immediately 7 causing redirection is that you have response 6 buffering on (the default) and the page 5 processing is not ended until after the 4 buffer is flushed. If this were true (and 3 admittedly I'm to lazy too try) then adding 2 the following line would also solve you 1 problem.

response.flush()
Score: 0

For an unconditional termination, you could 1 try a

Response.End()

More Related questions