What is the purpose of the Response.End() method? Is it necessary to call Response.End() after doing a Response.Redirect() ?
I am maintaining some C# code. I see that they use the Response.Redirect() method to transfer control to a different web page. Shortly after the Response.Redirect in the code, the Response.End() method is called. I don't know why though?
My guess without doing the actual test would be that maybe they are trying to avoid the
ThreadAbortException
This exception normally gets thrown when you redirect to another page. Is it inside of a try, catch loop? Just a guess, not 100% on whether this avoids the exception
though. Anyone know for sure?
Ok, I couldn't avoid doing the test for my own knowledge, the answer is that Response.End(); is never getting called because the actual Response.Redirect is the one the method thats throwing the exception. So whether you are using at try catch or not, the Response.End();
is never getting called and unnecessary.
Response.Redirect sends a 302 response header to the browser telling the browser the object has moved. Response.End ends the page execution. The thing is that Response.End is called internally by Response.Redirect so there is no need to call it seperately,
in fact it will cause an error. Here are a couple articles that might shed more light on this.
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
The default behavior of Response.Redirect is to send the redirect to the client and end the response which aborts the thread.
in this example, Response.End will not get called since the thread will automatically abort on the redirect.
This behavior can be changed though by using the Redirect overload to instruct that method not to automatically end the response.
Response.Redirect("somePage.aspx", False) 'dont end the response just yet
'now do a little more work
'...
Response.End() 'now end the response without completing the rest of the page lifecycle
To avoid the ThreadAbortException use the overloaded method. Response.Redirect("Target.aspx", true);
As explained by mbanavige, in order to avoid the ThreadAbortException you would actually call the overloaded constructor but with a value of false, not true. Where a false tells the Response.Redirect
method to not abort the thread (in other words not call Response.End() which is what really is responsible for this exception) This works great, now I can get rid of all of my empty ThreadAbortException Catch Statements!
now I can get rid of all of my empty ThreadAbortException Catch Statements!
There are 2 side effects to be aware of though which you should consider before making that change.
1) the page code (full lifecycle) will continue to run consuming system resources. Imagine how much unnecessary code might run if the redirect occurred in page_load.
2) after the redirect, its possible that you have some logic (logging perhaps or something...) that would be innapropriate to run if the page wasnt actually sent to the client browser.
brgdotnet
Participant
1184 Points
704 Posts
Purpose of the Response.End(); method ?
Jan 15, 2007 06:25 PM|LINK
What is the purpose of the Response.End() method? Is it necessary to call Response.End() after doing a Response.Redirect() ?
I am maintaining some C# code. I see that they use the Response.Redirect() method to transfer control to a different web page. Shortly after the Response.Redirect in the code, the Response.End() method is called. I don't know why though?
Example:
Response.Redirect(Page.ResolveUrl("~/" + theWebPage));
Response.End();
Snowst
Participant
905 Points
203 Posts
Re: Purpose of the Response.End(); method ?
Jan 15, 2007 06:33 PM|LINK
Snowst
Participant
905 Points
203 Posts
Re: Purpose of the Response.End(); method ?
Jan 15, 2007 06:48 PM|LINK
webdevl
Member
177 Points
38 Posts
Re: Purpose of the Response.End(); method ?
Jan 15, 2007 06:55 PM|LINK
Response.Redirect sends a 302 response header to the browser telling the browser the object has moved. Response.End ends the page execution. The thing is that Response.End is called internally by Response.Redirect so there is no need to call it seperately, in fact it will cause an error. Here are a couple articles that might shed more light on this.
http://support.microsoft.com/kb/312629
http://haacked.com/archive/2004/10/06/1308.aspx
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: Purpose of the Response.End(); method ?
Jan 15, 2007 10:55 PM|LINK
Response
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
Jessica Cao...
All-Star
25329 Points
2567 Posts
Re: Purpose of the Response.End(); method ?
Jan 16, 2007 12:41 AM|LINK
hi,
Response.End()' s behavior has some new behavior in asp.net2.0
for more information
refer to this thread
http://west-wind.com/WebLog/posts/3417.aspx
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
satish_nagdev
Contributor
6572 Points
1432 Posts
Re: Purpose of the Response.End(); method ?
Jan 16, 2007 12:56 AM|LINK
hi,
could you tell what error you are getting?
just comment response.end() and try.
thanks,
satish.
mbanavige
All-Star
130696 Points
14322 Posts
ASPInsiders
Moderator
MVP
Re: Purpose of the Response.End(); method ?
Jan 16, 2007 12:57 AM|LINK
The default behavior of Response.Redirect is to send the redirect to the client and end the response which aborts the thread.
in this example, Response.End will not get called since the thread will automatically abort on the redirect.
Response.Redirect(Page.ResolveUrl("~/" + theWebPage)); Response.End();This behavior can be changed though by using the Redirect overload to instruct that method not to automatically end the response.
Snowst
Participant
905 Points
203 Posts
Re: Purpose of the Response.End(); method ?
Jan 16, 2007 02:05 AM|LINK
As explained by mbanavige, in order to avoid the ThreadAbortException you would actually call the overloaded constructor but with a value of false, not true. Where a false tells the Response.Redirect method to not abort the thread (in other words not call Response.End() which is what really is responsible for this exception) This works great, now I can get rid of all of my empty ThreadAbortException Catch Statements!
mbanavige
All-Star
130696 Points
14322 Posts
ASPInsiders
Moderator
MVP
Re: Purpose of the Response.End(); method ?
Jan 16, 2007 02:18 AM|LINK
There are 2 side effects to be aware of though which you should consider before making that change.
1) the page code (full lifecycle) will continue to run consuming system resources. Imagine how much unnecessary code might run if the redirect occurred in page_load.
2) after the redirect, its possible that you have some logic (logging perhaps or something...) that would be innapropriate to run if the page wasnt actually sent to the client browser.