Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:

I just encountered “System.Threading.ThreadAbortException” exception if I write Response.Redirect in a try block. See below the code I have written.


C#
protected void btnVisitSecondPage_Click(object sender, EventArgs e)

Response.Redirect("frmSecondPage.aspx");
}
catch (Exception ex) { } 
} 

Above code is working fine but if I debug it, I can see Response.Redirect throws “System.Threading.ThreadAbortException” exception and gets resolved by the framewrok autonmatically.

Most strange part is that if I remove the try block then it also works fine without throwing an exception .

See below the code
C#
protected void btnVisitSecondPage_Click(object sender, EventArgs e)Response.Redirect("frmSecondPage.aspx");

Can any one please give me the explanation ?
Posted
Updated 12-Feb-21 1:09am
v10

Read this article: http://support.microsoft.com/kb/312629/EN-US/ 

I hope it explains your problem. 

 
Share this answer
 

I have just tried a new web application with 2 pages,

Page1 had a button with a response.redirect to page2

Page2 had a button with a response.redirect within a try block to page1.

It works fine without any issue.

 
Share this answer
 
v2
Response.Redirect always throws a excpetion.
Best thing to do is catch the type of exception you want.
 
Share this answer
 
Comments
CHill60 5-Dec-13 10:43am    
You do realise that this question is 4 years old?
SoMad 6-Dec-13 3:22am    
That has to be some kind of record :). Maybe someone should tell Dave that he has a message waiting in Solution 2 :).

Soren Madsen
phil.o 5-Dec-13 10:56am    
Nice post-digging!
Hi Daveauld, Thanks for your response. Yes, this is true. Even this is working for me also. But if you set a debug point and press F11 key to run your example. Then you can see Response.Redirect is throwing an execption which is getting resolved automatically and at last Response.Redirect is working. Looking forward from hearing to you.
 
Share this answer
 
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();


a href="http://stackoverflow.com/questions/2777105/why-response-redirect-causes-system-threading-threadabortexception
 
Share this answer
 
Response.Redirect("Home.aspx", false);
Context.ApplicationInstance.CompleteRequest();
 
Share this answer
 
Comments
CHill60 15-Feb-21 10:40am    
Copy of Solution 5. Don't copy previous posts

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900