为什么的Response.Redirect导致System.Threading.ThreadAbortException? [英] Why Response.Redirect causes System.Threading.ThreadAbortException?
问题描述
当我使用的Response.Redirect(...),以我的形式重定向到一个新的页面出现错误:
When I use Response.Redirect(...) to redirect my form to a new page I get the error:
有一个第一次机会异常的类型'System.Threading.ThreadAbortException的在mscorlib.dll
发生
类型'System.Threading.ThreadAbortException的一个例外,在mscorlib.dll中发生,但在用户code没有处理
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code
我的这种认识是错误可能出自网络服务器中止了的Response.Redirect被称为在页面的其余部分。
My understanding of this is that the error is being caused by the webserver aborting the remainder of the page the response.redirect was called on.
我知道我可以添加第二个参数的Response.Redirect
被称为endResponse。如果我设置endResponse为True我仍然得到错误,但如果我将它设置为false,那么我不知道。我是pretty的肯定,虽然这意味着该Web服务器正在运行我重定向离开该页面的其余部分。这似乎是低效的,至少可以说。有没有更好的办法做到这一点?其他的东西比的Response.Redirect
还是有办法迫使旧页面,停止加载,我不会得到一个 ThreadAbortException
?
I know I can add a second parameter to Response.Redirect
that is called endResponse. If I set endResponse to True I still get the error but if I set it to False then I do not. I am pretty sure though that that means the webserver is running the rest of the page I redirected away from. Which would seem to be inefficient to say the least. Is there a better way to do this? Something other than Response.Redirect
or is there a way to force the old page to stop loading where I will not get a ThreadAbortException
?
推荐答案
正确的模式是先调用重定向超负荷endResponse =虚假和拨打电话,告诉IIS管道,它应该直接推进到EndRequest阶段,一旦你返回控制:
The correct pattern is to call the Redirect overload with endResponse=false and make a call to tell the IIS pipeline that it should advance directly to the EndRequest stage once you return control:
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();
<一个href="http://blogs.msdn.com/tmarq/archive/2009/06/25/correct-use-of-system-web-htt$p$psponse-redirect.aspx">This从托马斯·马夸特博客帖子提供了额外的细节,包括如何处理重定向一个的Application_Error处理程序内的特殊情况。
This blog post from Thomas Marquardt provides additional details, including how to handle the special case of redirecting inside an Application_Error handler.
这篇关于为什么的Response.Redirect导致System.Threading.ThreadAbortException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!