61

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.

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98

5 Answers5

148

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
m-zemmouri
  • 123
  • 8
David
  • 208,112
  • 36
  • 198
  • 279
23

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.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • 1
    This is the recommended approach, you just need to be careful using this as code after the Response.Redirect will be executed. – Mike Ohlsen Feb 02 '11 at 13:25
  • 18
    After calling Response.Redirect(url, false) call HttpContext.Current.ApplicationInstance.CompleteRequest(); to terminate without an exception. – StanleyH Oct 10 '11 at 15:03
  • 1
    But the Page lifecycle continues, Postback events are handled, and html is sent to the client. See http://www.c6software.com/CodeSolutions/dotnet/ThreadAbortException.aspx – StanleyH Oct 10 '11 at 15:21
  • @StanleyH yes, After calling Response.Redirect(url, false), the following code in current page will be run, this is not my purpose. how to resolve it. – paul cheung Mar 20 '18 at 02:24
  • @StanleyH No, it doesn't, the request continues with it's page life cycle. – ActiveX Jul 08 '20 at 21:15
6

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

Christopher
  • 1,639
  • 19
  • 22
0

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

jkdev
  • 11,360
  • 15
  • 54
  • 77
gor
  • 11,498
  • 5
  • 36
  • 42
0

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

Raphael Mutiso
  • 63
  • 1
  • 11