vb.net Response.redirect 引发“线程被中止”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4874823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Response.redirect raises "Thread was being aborted"
提问by Andrea Girardi
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
当我需要从数据库中删除一个对象时,我调用了一个 VB.NET 代码。在 Page_load 上,我检查它是否没有回发(以防止手动刷新),并且在删除对象后,我使用 Response.redirect 重定向到调用方页面。此时我的代码引发了一个
exception:EXCEPTION OCCURS In File_delete.aspx.vb Line Number: 34 Error Message: Thread was being aborted.
异常:异常发生在 File_delete.aspx.vb 行号:34 错误消息:线程被中止。
and, on Event Viewer I can see that aspnet_wp.exe crashes:
并且,在事件查看器上,我可以看到 aspnet_wp.exe 崩溃了:
aspnet_wp.exe (PID: 1532) stopped unexpectedly.
For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
aspnet_wp.exe (PID: 1532) 意外停止。
有关详细信息,请参阅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.
不清楚为什么只在这里发生这种情况,因为我还使用 response.redirect 来查看文件,而不仅仅是删除它。
回答by David
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()
中止当前线程。当然,这会抛出一个ThreadAbortException
. 可以通过传递false
to来防止它Response.Redirect()
,这不会中止当前线程。
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()
将继续执行。相应地控制您的逻辑流程。(这通常通过return
重定向后的语句和其他流控制指令来完成。)
回答by Matías Fidemraizer
Response.Redirect
will always throw a ThreadAbortException
, according to MSDN documentationif you don't give a false
boolean value to endResponse
input parameter HttpRequest.Redirect(string, bool)
.
Response.Redirect
ThreadAbortException
根据MSDN 文档,如果您不false
为endResponse
输入参数提供布尔值,将始终抛出HttpRequest.Redirect(string, bool)
。
Just give false
to endResponse
parameter.
只要给false
到endResponse
的参数。
回答by Chris D. Emerson
The list of options for solving this issue laid out here worked for me (I used #2): https://gist.github.com/cemerson/9dea993044a4e7fdca0e
此处列出的解决此问题的选项列表对我有用(我使用了 #2):https: //gist.github.com/cemerson/9dea993044a4e7fdca0e
回答by Raphael Mutiso
This could happens when you are making asynchronous calls. Use Response.Redirect(string url, false) where string url is the redirect url.
当您进行异步调用时可能会发生这种情况。使用 Response.Redirect(string url, false) 其中 string url 是重定向 url。
回答by gor
Response.Redirect
throws an exception by design. It is OK.
Response.Redirect
按设计抛出异常。没关系。