简体   繁体   English

什么时候应该使用 Response.Redirect(url, true)?

[英]When Should I Use Response.Redirect(url, true)?

I'm redirecting to an Error page with a prettified error message in my Application_Error , in Global.asax.我在 Global.asax 中的Application_Error中重定向到带有美化错误消息的错误页面。

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 ?我不确定在什么情况下应该使用true以及应该使用false The MSDN page says to prefer using false to avoid ThreadAbortExceptions, so when should I use true ? MSDN 页面说更喜欢使用false来避免 ThreadAbortExceptions,所以我应该什么时候使用true

You use false when you don't want to abort the thread.当您不想中止线程时使用false What that means is that false will cause the code to continue to execute.这意味着false将导致代码继续执行。 So lines of code which appear after the Response.Redirect will be executed.因此将执行Response.Redirect之后出现的代码行。 A true will just kill the thread so nothing further will execute, which in turn throws a ThreadAbortException . true只会杀死线程,因此不会执行任何进一步的操作,这反过来会引发ThreadAbortException

So it's really a judgment call based on how the rest of the code in that situation looks.因此,这实际上是基于该情况下代码的 rest 外观的判断调用。 Generally you want to put calls to Response.Redirect at the end of an execution path so that nothing further needs to be executed.通常,您希望将Response.Redirect调用放在执行路径的末尾,这样就不需要执行任何进一步的操作。 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 a return and the execution path simply ends, then you're probably fine.例如,如果Response.Redirect之后的下一行是return并且执行路径简单地结束,那么您可能没问题。 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.但是如果有各种逻辑并且在这种情况下执行它会使系统处于未知的 state 中,那么您可能想要中止线程。

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(URL,false): The client is redirected to a new page and the current page on the server will keep processing ahead. Response.Redirect(URL,false):客户端被重定向到一个新页面,服务器上的当前页面将继续处理。

Response.Redirect(URL,true): The client is redirected to a new page, but the processing of the current page is aborted. Response.Redirect(URL,true):客户端被重定向到一个新页面,但是当前页面的处理被中止。

You can also see this video which demonstrates the differences Response.Redirect ( False vs True) ASP.NET Interview questions with answers .您还可以观看此视频,该视频演示了Response.Redirect (False vs True) ASP.NET 面试问题和答案的差异。

You never need to use true , as there is an overload without the boolean parameter.您永远不需要使用true ,因为没有 boolean 参数会出现过载。

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

or或者

Response.Redirect("Error.aspx");

The boolean parameter was added so that you could set the redirect without stopping the execution.添加了 boolean 参数,以便您可以在不停止执行的情况下设置重定向。 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.如果您可以自己退出页面代码而不会导致任何额外成本,例如发生数据绑定,那是可取的。

Here it is best to use true , because you want all the other threads to abort;这里最好使用true ,因为您希望所有其他线程中止; there has been an error and the application cannot continue.出现错误,应用程序无法继续。

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.如果设置为true,应用程序将结束响应并将其返回给用户,如果设置为false,重定向后的代码将继续执行,用户将被重定向到新页面完整的页面加载生命周期结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Response.Redirect 时如何删除段 URL - How to remove segment URL when use Response.Redirect 使用Response.Redirect(“ url”,true)时执行线程如何终止? - How the executing thread gets terminated when Response.Redirect(“url”,true) is used.? 使用Response.Redirect时获取附加的URL - URL getting Appended when using Response.Redirect 何时最好使用false作为Response.Redirect的第二个参数,何时不使用? - When is it best to use false as second parameter of Response.Redirect and when not? EndResponse是真正的response.redirect在try catch中 - EndResponse is true response.redirect in try catch 当我将WebClient与Silverlight 4.0一起使用时无法执行Response.Redirect - Can not do Response.Redirect when I use WebClient with Silverlight 4.0 to call aspx page 当我使用Response.Redirect()或Server.Transfer()时,我的用户被注销 - When I use Response.Redirect() or Server.Transfer() my user gets logged out 如何在response.redirect上使用Target = _blank? - How do I use Target=_blank on a response.redirect? 我可以在不编码的情况下使用 Response.Redirect 吗? - Can I use Response.Redirect without encoding? 哪种是重定向URL的最佳方法? 我们应该使用Response.Redirect还是有其他用于重定向的命令? - Which is the best way to redirect URL? Should we be using Response.Redirect or are there any other commands for redirecting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM