Response.Redirect not ending execution - asp.net

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?

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.

are you exiting from the function that calls redirect, e.g.
...redirect(stopit,true);
return;
?

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.

You can throw an exception, that will exit code execution, but still redirect:
HttpContext.Current.Response.Redirect("/login", true);
throw new Exception("Unauthorized Access");

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

For an unconditional termination, you could try a
Response.End()

Related

Response.Redirect("XXX", false) and Page Life Cycle?

Due to performance reason I am using, Response.Redirect("XXX", false). I don't want page-life-cycle to continue. Is there is any way to use Response.Redirect("XXX", false)(note the false) and stop page life-cycle in the same time. See the performace issue,
http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx
http://mvolo.com/fix-the-3-high-cpu-performance-problems-for-iis-aspnet-apps/
You can always use Response.End(), but the easiest way is to simply pass the true argument.
Response.Redirect("XXX", true);
Or
Response.Redirect("XXX", false);
Response.End();
EDIT:
After reading comments on your post, it seems like Response.End() will not be useful either, because it will also throw a ThreadAbortException.
It seems however that the best answer is given here (use HttpContext.Current.ApplicationInstance.CompleteRequest instead of response end, but it will not stop the page lifecycle): http://support.microsoft.com/kb/312629/en
Response.Redirect("XXX", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
Best thing to do if you do not want the exception is to then exit gracefully in your code.

Page transfer in ASP.NET

In Response.redirect ("Page.aspx",bool end response), How do I transfer the page and come back to to the same execution point?
I mean to say how can I use the bool value for my programming purpose.
Please let me know
If i understand "come back to the same execution point" correctly, you might consider using Server.Execute instead.
From MSDN
Executes the handler for a specified resource in the context of the
current request and returns execution to the page that invoked it.
I think you can create a session variable and stock the "starting point"(url) in the variable.
After that you can get the "starting point" from anywhere and go back to this page..
(If it is want you want to do..)
You could to start run all your operation in another thread and then only do response.redirect("someurl"), i.e.
reponse.redirect("some.aspx");
myoperation();
myoperation1();
replace this code on
ThreadPool.QueueUserWorkItem(delegate
{
myoperation();
myoperation1();
});
Response.Redirect("some.aspx");
To answer your specific questions:
you cannot use the bool parameter for your own purposes - it is there to signal whether to end the response back to the client (msdn link)
when you redirect or transfer to a page you still need to go through the page life cycle, you cannot just start at some arbitrary bit of code on the page. As already mentioned in the answer from InSane, use the Execute method instead.

Using endResponse in a Response.Redirect

While performing a response.redirect in an ASP.NET page, I received the error:
error: cannot obtain value
for two of the variables being passed in (one value being retrieved from the querystring, the other from viewstate)
I've never seen this error before, so I did some investigating and found recommendations to use the "False" value for "endResponse"
e.g. Response.Redirect("mypage.aspx", False)
This worked.
My question is: what are the side-effects of using "False" for the "endResponse" value in a response.redirect?
i.e. are there any effects on the server's cache? Does the page remain resident in memory for a period? Will it affect different users viewing the same page? etc.
Thanks!
From this other question and this blog post, the recommended pattern is-
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();
This avoids the expensive ThreadAbortException/Response.End. Some code will be executed after the CompleteRequest(), but ASP.Net will close the request as soon as it is convenient.
Edit- I think this answer gives the best overview. Note that if you use the pattern above, code after the redirect will still be executed.
An MSDN blog post that might answer your question:
The drawback to using [Response.Redirect(url, false)] is that the page will continue to process on the server and be sent to the client. If you are doing a redirect in Page_Init (or like) and call Response.Redirect(url, false) the page will only redirect once the current page is done executing. This means that any server side processing you are performing on that page WILL get executed.
Response.Redirect(..., true) results in a ThreadAbortException.
depending on your exception handling setup you might get your log filled with error messages one for each redirect.

Responce.redirect error

In the login check of my personal webpage i am using
Response.Redirect("~/ClientCenter/Default.aspx")
and get the Property evaluation failed error
I guess i should set the overloaded option endResponce = False
...but for what reason?
The solution for your problem is here
You might want to consider checking that URL of yours. The tilde might not be acceptable.
see Response.redirect call the Response.End which internally call Applicaiton_End Event n this result your current thread get aborted forcefully.... To avoid this v use overloaded Response.redirect with false parameter which says no to Response.End.
I hope u got some clue..

Error: The HTTP headers are already written to the client browser

I got this error
Response object error 'ASP 0156 : 80004005'
Header Error
/ordermgmt/updateorderstatus.asp, line 1390
The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
I put Response.Buffer=true;
Stilll it is showing error.
I have put reponse,Redirect # this line number and that will be executed a number of times (it is in a loop).,After the first iteration it is showing this error
Yes buddies, Its Fixed.Before Response.Buffer ,i included another file.Now i changed it to below the Response.Buffer=True line .Its working now .Thanks
Check that you're not outputting anything at all - even a blank line before your start ASP tag will cause this problem.
The first Response.Redirect changes the headers (and probably forces a Flush, because with a redirect, there can be no content).
The second Response.Redirect changes the headers again (probably to the same thing, but that doesn't matter, as the header were written during the Flush())
You have to enable buffering on specific page, then you can remove this error like:
<% Response.Buffer = True %>
on top of the ASP form
The same applies if you are using Response.Flush()
Set the property storage enabled = true in IIS > ASP > Storage enabled

Resources