Response.Redirect not ending execution

I have Default.aspx page, which inherits from BasePage.cs, which inherits from System.Web.UI.Page. BasePage is where I do some common things every page must do upon loading.

In BasePage, lets say I’m checking for X. If X=1, then I will redirect to my “Discontinued.aspx” page immediately and stop execution of BasePage. If I find X=1, I say:

HttpContext.Current.Response.Redirect(“Discontinued.aspx”, true);

I want the redirect to stop execution of BasePage and immediately jump out – hence the “true” in the above statement – which should stop execution of the current page as I understand. The problem is, it doesn’t. I’m expecting the redirect to throw the “thread abort exception”.

When I run in debug mode, it contines stepping through as though it didn’t just redirect and leave.

But the redirect was still started as well – once I get done stepping through the rest of BasePage, the “Discontinued” page then begins to load as a result of the redirect.

Is there a reason my Redirect will not kill execution of BasePage?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

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

Method 2

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

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

?

Method 3

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

Method 4

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

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

Method 5

I’m going to get down voted for this! Curse my ignorance…

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

response.end()

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

Method 6

For an unconditional termination, you could try a

Response.End()


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x