Chad
Chad

Reputation: 561

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?

Upvotes: 18

Views: 28442

Answers (6)

Vildan
Vildan

Reputation: 1994

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

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

Upvotes: 3

Shawn
Shawn

Reputation: 19813

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.

Upvotes: 37

Christopher Edwards
Christopher Edwards

Reputation: 6659

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()

Upvotes: 1

recursive
recursive

Reputation: 86094

For an unconditional termination, you could try a

Response.End()

Upvotes: 0

Jaime Febres
Jaime Febres

Reputation: 1267

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.

Upvotes: 3

Steven A. Lowe
Steven A. Lowe

Reputation: 61243

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

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

?

Upvotes: 12

Related Questions