为什么 Response.Redirect 会导致 System.Threading.ThreadAbortException? [英] Why Response.Redirect causes System.Threading.ThreadAbortException?

查看:26
本文介绍了为什么 Response.Redirect 会导致 System.Threading.ThreadAbortException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 Response.Redirect(...) 将表单重定向到新页面时,出现错误:

When I use Response.Redirect(...) to redirect my form to a new page I get the error:

在 mscorlib.dll 中发生了System.Threading.ThreadAbortException"类型的第一次机会异常
mscorlib.dll 中出现System.Threading.ThreadAbortException"类型的异常,但未在用户代码中处理

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 那么我不会.我很确定这意味着网络服务器正在运行我重定向的页面的其余部分.至少可以说这似乎是低效的.有一个更好的方法吗?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=false 调用 Redirect 重载,并调用告诉 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();

此博客来自 Thomas Marquardt 的帖子 提供了更多详细信息,包括如何处理在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆