我什么时候应该使用 Response.Redirect(url, true)?
我正在重定向到一个错误页面,并在 Global.asax 的 Application_Error
中显示一条经过美化的错误消息。
目前它说:
Response.Redirect("Error.aspx", true);
应该是:
Response.Redirect("Error.aspx", false);
我不确定在哪种情况下应该使用 true
以及哪种情况下应该使用 false
? MSDN 页面 表示更喜欢使用 false
为了避免 ThreadAbortExceptions,那么什么时候应该使用 true
?
I'm redirecting to an Error page with a prettified error message in my Application_Error
, in Global.asax.
At the moment it says:
Response.Redirect("Error.aspx", true);
Should that be:
Response.Redirect("Error.aspx", false);
I'm not sure under which circumstances I should use true
and which I should use false
? The MSDN page says to prefer using false
to avoid ThreadAbortExceptions, so when should I use true
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果设置为true,应用程序结束响应并将其发送回用户,如果设置为false,则重定向后的代码将继续执行,用户将被重定向到新的页面。完整页面加载生命周期结束。
If you set it to true, the application ends the response and sends it back to the user, and if you set it to false the code after the redirect will continue to be executed, and the user will be redirected to the new page after the full page-load life cycle ends.
Response.Redirect(URL,false):客户端重定向到新页面,当前页面位于服务器将继续处理。
Response.Redirect(URL,true):客户端被重定向到新页面,但当前页面的处理被中止。
您还可以观看此视频,其中演示了Response.Redirect(False 与 True)ASP 的差异。 NET 面试问题及答案。
Response.Redirect(URL,false): The client is redirected to a new page and the current page on the server will keep processing ahead.
Response.Redirect(URL,true): The client is redirected to a new page, but the processing of the current page is aborted.
You can also see this video which demonstrates the differences Response.Redirect ( False vs True) ASP.NET Interview questions with answers.
您永远不需要使用
true
,因为没有布尔参数会发生重载。或者
添加了布尔参数,以便您可以在不停止执行的情况下设置重定向。如果您可以自行退出页面代码,而不会造成任何额外成本(例如发生数据绑定),那就更好了。
You never need to use
true
, as there is an overload without the boolean parameter.or
The boolean parameter was added so that you could set the redirect without stopping the execution. If you can exit out of the page code yourself without that causing any extra cost, like for example data binding occuring, that is preferable.
这里最好使用
true
,因为您希望所有其他线程中止;出现错误,应用程序无法继续。Here it is best to use
true
, because you want all the other threads to abort; there has been an error and the application cannot continue.当您不想中止线程时,可以使用
false
。这意味着false
将导致代码继续执行。因此出现在 Response.Redirect 后面的代码行将被执行。true
只会终止线程,因此不会再执行任何操作,从而引发ThreadAbortException
。所以这实际上是一个基于该情况下其余代码的外观的判断调用。通常,您希望将对 Response.Redirect 的调用放在执行路径的末尾,以便不再需要执行任何操作。但很多时候事实并非如此。这只是如何控制代码中的逻辑流程的问题。
例如,如果
Response.Redirect
之后的下一行是return
并且执行路径只是结束,那么您可能没问题。但是,如果存在各种逻辑并且在这种情况下执行它会使系统处于未知状态,那么您可能想要中止该线程。我个人认为中止线程表明逻辑控制不佳。它类似于众所周知的代码味道,其中使用异常来控制逻辑流,这是普遍不赞成的。如果您可以控制逻辑流而不需要中止线程并引发异常,那么这可能是首选。
You use
false
when you don't want to abort the thread. What that means is thatfalse
will cause the code to continue to execute. So lines of code which appear after theResponse.Redirect
will be executed. Atrue
will just kill the thread so nothing further will execute, which in turn throws aThreadAbortException
.So it's really a judgment call based on how the rest of the code in that situation looks. Generally you want to put calls to
Response.Redirect
at the end of an execution path so that nothing further needs to be executed. But many times that's not the case. It's just a matter of how you control the logic flow in the code.For example, if the next line after
Response.Redirect
is areturn
and the execution path simply ends, then you're probably fine. But if there's all kinds of logic and executing it in this case would leave the system in an unknown state, then you may want to abort the thread.Personally I consider aborting the thread to be indicative of poor logic control. It's similar to a well known code smell where exceptions are used to control logic flow, which is universally frowned upon. If you can control the logic flow without the need for aborting a thread and throwing an exception, that would probably be preferred.