gpt4 book ai didi

.net - (""内的 Response.Redirect "using{ }")

转载 作者:行者123 更新时间:2023-12-01 09:11:49 26 4
gpt4 key购买 nike

假设以下代码:

using (SqlConnection conn = new SqlConnection(connectionString))
{
...
using (SqlCommand comm = new SqlCommand(...))
{
.. do stuff ..
if(condition) Response.Redirect("somepage.aspx");

}
}

Response.Redirect() 从 using 块退出会导致它处理所有连接吗?

或者,有没有办法退出不会导致处置的 using 块?

编辑:我不想在没有处置的情况下退出。我想知道任何会导致它不起作用的陷阱。 -- 当然,除非崩溃,但我很确定所有对象都被处理了 -- 在这种情况下,很难

我接受了一个答案,基本上说“我不知道”,但这是一个经过充分研究的“我不知道”

同时,我将假设 Response.Redirect 中止 using 语句和代码并考虑到这一点。 ——除非另有证明。

最佳答案

来自 http://msdn.microsoft.com/en-us/library/aa973248.aspx

Calling Response.Redirect WILL NOT execute the finally block. Therefore, before any redirection or transfer of processing can occur, you must dispose of the objects.



是的,它没有直接解决 Using 语句,但它是一种足够常见的编程实践,需要注意。另外,那篇文章提到了 SharePoint,但由于 SP 是基于 ASP.NET 2.0 构建的,我认为它仍然具有相关性。

关于.net - (""内的 Response.Redirect "using{ }"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/187189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com