Asp.net

Response.Redirect 和執行緒被中止錯誤?

  • December 7, 2014

我有這個錯誤執行緒被中止。,今天下午在我的錯誤日誌中。

導致此錯誤的程式碼是:

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

如果我將bool值更改為false,錯誤日誌將變為空並且此錯誤不再出現,但程序停止工作。

如果我保持這種狀態,我會收到類似滋擾的錯誤。

我想知道使用Response.Redirect傳遞true作為endResponse參數值的替代方法。

我捕捉到這個異常併吞下它,因為 ASP.NET 使用異常來進行流控制,而不是用於異常情況。

try
{
   // Do stuff.
}
catch(ThreadAbortException)
{
   // Do nothing. ASP.NET is redirecting.
   // Always comment this so other developers know why the exception 
   // is being swallowed.
}
catch(OtherExceptionTypes ex)
{
   // Log other types of exception.
}

Response.Redirect(url)ThreadAbortException 解決方案中所述:

呼叫時會拋出 ThreadAbortException, Response.Redirect(url)因為系統在將重定向發送到響應流後中止了目前網頁執行緒的處理。Response.Redirect(url)實際上是在Response.End() 內部進行呼叫,正是Response.End()這些呼叫Thread.Abort()使堆棧冒泡以結束執行緒。在極少數情況下,呼叫Response.End()實際上不呼叫Thread.Abort(),而是呼叫HttpApplication.CompleteRequest()

或者乾脆移出Response.Redirect("~/Membership/UserRegistration.aspx");Try/Catch 塊。

引用自:https://stackoverflow.com/questions/4368640