的Response.Redirect("")的内部及QUOT;使用{}"部及、Response、Redirect、QUOT

2023-09-03 06:48:06 作者:身心疲惫

假设以下code:

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

   }
}

将从使用块原因它的Response.Redirect()退出来处理所有的连接?

Will the Response.Redirect() exit from the using blocks cause it to dispose all connections?

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

Or, alternatively, is there any way to exit a using block that won't cause disposal?

编辑:我不想过没有处理退出。我想知道的任何缺陷,会导致其无法正常工作。 - 除非当然崩溃,但我敢肯定,所有的对象被设置--the硬方式 - 在这种情况下pretty的

I don't want to exit without disposal. I want to be aware of any pitfalls that would cause it not to work. -- Barring crashes of course, but then I'm pretty sure all objects are disposed --the hard way-- in that situation

我已经接受了一个答案,基本上说:我不知道,但它是一个很好的研究,我不知道。

I've accepted an answer, which essentially says "I don't know" but it's a very well researched "I don't know"

有关的平均时间,我会假设的Response.Redirect中止使用的语句和code考虑到这一点。 - 直到证明不是

For the mean time, I'm going to assume that Response.Redirect aborts the using statement and code with that in mind. -- Until proven otherwise.

推荐答案

从 HTTP ://msdn.microsoft.com/en-us/library/aa973248.aspx

调用的Response.Redirect不会   执行finally块。因此,   任何重定向或转移之前,   处理发生时,您必须配置   对象

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.

是的,它并不直接处理使用语句,但它是一种常见足够的编程习惯要注意的。此外,该文章引用到SharePoint,但由于SP是建立在ASP.NET 2.0中,我认为它仍然是相关的。

Yes, it does not directly address the Using statement, but it is a common enough programming practice to be aware of. Also, that article refers to SharePoint, but as SP is built on ASP.NET 2.0, I think it is still relevant.