Andrea Girardi
Andrea Girardi

Reputation: 4427

Response.redirect raises "Thread was being aborted"

I've a VB.NET code called when I need to delete an object from DB. On Page_load I check if it's not post back (to prevent a manual refresh) and, after the deletion of the object I redirect to the caller page using Response.redirect. At this point my code raise an

exception:EXCEPTION OCCURS In File_delete.aspx.vb Line Number: 34 Error Message: Thread was being aborted.

and, on Event Viewer I can see that aspnet_wp.exe crashes:

aspnet_wp.exe (PID: 1532) stopped unexpectedly.

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

It's not clear why this happens only here because I use the response.redirect also to view the file and not only to delete it.

Upvotes: 64

Views: 77477

Answers (5)

David
David

Reputation: 218798

By default, Response.Redirect() aborts the current thread. Naturally, this throws a ThreadAbortException. It can be prevented by passing a false to Response.Redirect(), which won't abort the current thread.

Response.Redirect(url,false) 

Be aware of what that means, however. If the thread is not aborted, the code following the Response.Redirect() will continue to execute. Control your logic flow accordingly. (This is often done with return statements and other flow control directives following a redirect.)

Response.Redirect(url,false) 
Return

Upvotes: 155

Raphael Mutiso
Raphael Mutiso

Reputation: 71

This could happens when you are making asynchronous calls. Use Response.Redirect(string url, false) where string url is the redirect url.

Upvotes: 0

gor
gor

Reputation: 11658

Response.Redirect throws an exception by design. It is OK.

Upvotes: 0

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64933

Response.Redirect will always throw a ThreadAbortException, according to MSDN documentation if you don't give a false boolean value to endResponse input parameter HttpRequest.Redirect(string, bool).

Just give false to endResponse parameter.

Upvotes: 24

Christopher
Christopher

Reputation: 2256

The list of options for solving this issue laid out here worked for me (I used #2): https://gist.github.com/cemerson/9dea993044a4e7fdca0e

Upvotes: 6

Related Questions