我什么时候应该使用的Response.Redirect(网址,真实)? [英] When Should I Use Response.Redirect(url, true)?

查看:201
本文介绍了我什么时候应该使用的Response.Redirect(网址,真实)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重定向到了错误的网页在我的的Application_Error ,在Global.asax中一个prettified错误消息。

I'm redirecting to an Error page with a prettified error message in my Application_Error, in Global.asax.

目前,它说:

Response.Redirect("Error.aspx", true);

应该说是:

Response.Redirect("Error.aspx", false); 

我不知道在何种情况下,我应该使用真正和我应该使用?该 MSDN页面使用来避免ThreadAbortExceptions,所以当我应该使用真正

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?

推荐答案

您使用当你不想中止线程。这也就意味着,将导致code继续执行。因此,的Response.Redirect 将被执行后出现code线。 A 真正只会杀死线程,这样没有什么可以再会执行,这又抛出一个 ThreadAbortException

You use false when you don't want to abort the thread. What that means is that false will cause the code to continue to execute. So lines of code which appear after the Response.Redirect will be executed. A true will just kill the thread so nothing further will execute, which in turn throws a ThreadAbortException.

所以,它是真正基于code在这种情况下,其余的外观判断调用。通常你想在执行路径的末尾放到的Response.Redirect 的电话,以要执行什么进一步的需求。但是很多时候,这不是这种情况。这只是一个如何控制在code中的逻辑流程的问题。

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.

例如,如果在的Response.Redirect 下一行是收益和执行路径简单地结束,那么你可能罚款。但是,如果出现的各种逻辑,在这种情况下执行它会离开系统处于未知状态,那么你可能要中止的线程。

For example, if the next line after Response.Redirect is a return 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.

我个人考虑中止线程是指示差逻辑控制。它类似于众所周知code气味例外情形被用于控制逻辑流,这是普遍皱眉。如果你能控制,而不需要逻辑流程中止当前线程,并抛出一个异常,这很可能是preferred。

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.

这篇关于我什么时候应该使用的Response.Redirect(网址,真实)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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