Response.Redirect和线程被中止错误?

时间:2023-01-25 21:00:06

I had this error Thread was being aborted., this afternoon in my error log.

我有这个错误线程被中止了。今天下午在我的错误日志中。

The code that caused this error is:

导致此错误的代码是:

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

If I change the bool value to false, the error log becomes empty and this error stops coming again, but the program stops working.

如果我将bool值更改为false,则错误日志将变为空,此错误将再次停止,但程序将停止工作。

If I keep it as such, I am getting this error like nuisance.

如果我保持这样的话,我会发现这个错误就像滋扰一样。

I want to know the alternative for using Response.Redirect passing true as the value for the endResponse parameter.

我想知道使用Response.Redirect传递true作为endResponse参数的值的替代方法。

4 个解决方案

#1


17  

I catch this exception and swallow it because ASP.NET is using exceptions for flow control rather than for an exceptional circumstance.

我抓住这个异常并吞下它,因为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.
}

#2


3  

As stated in Response.Redirect(url) ThreadAbortException Solution:

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

The ThreadAbortException is thrown when you make a call to Response.Redirect(url) because the system aborts processing of the current web page thread after it sends the redirect to the response stream. Response.Redirect(url) actually makes a call to Response.End() internally, and it's Response.End() that calls Thread.Abort() which bubbles up the stack to end the thread. Under rare circumstances the call to Response.End() actually doesn't call Thread.Abort(), but instead calls HttpApplication.CompleteRequest().

当您调用Response.Redirect(url)时抛出ThreadAbortException,因为系统在将重定向发送到响应流后中止当前网页线程的处理。 Response.Redirect(url)实际上是在内部调用Response.End(),而它的Response.End()调用Thread.Abort(),它会使堆栈冒泡以结束线程。在极少数情况下,对Response.End()的调用实际上不会调用Thread.Abort(),而是调用HttpApplication.CompleteRequest()。

Or simply move Response.Redirect("~/Membership/UserRegistration.aspx"); out of the Try/Catch block.

或者只需移动Response.Redirect(“〜/ Membership / UserRegistration.aspx”);超出Try / Catch块。

#3


2  

you can change like this Response.Redirect ("Login.aspx",false) then it wont abort.

你可以改变这样的Response.Redirect(“Login.aspx”,false)然后它不会中止。

#4


1  

For all caught errors where you want to redirect, create a 'GoTo' destined out of the Try Catch as follows:

对于要重定向的所有捕获错误,请创建一个从Try Catch注定的“GoTo”,如下所示:

    Try 

       'do stuff

    Catch ex As Exception

        'logging
        GoTo MyRedirection

    End Try

    'Prevent redirection in case of no errors
    Exit Sub

MyRedirection:
    Response.Redirect("login.aspx", True)

This neither causes thread abortion nor requires multiple catches.

这既不会导致线程中止,也不需要多次捕获。

#1


17  

I catch this exception and swallow it because ASP.NET is using exceptions for flow control rather than for an exceptional circumstance.

我抓住这个异常并吞下它,因为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.
}

#2


3  

As stated in Response.Redirect(url) ThreadAbortException Solution:

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

The ThreadAbortException is thrown when you make a call to Response.Redirect(url) because the system aborts processing of the current web page thread after it sends the redirect to the response stream. Response.Redirect(url) actually makes a call to Response.End() internally, and it's Response.End() that calls Thread.Abort() which bubbles up the stack to end the thread. Under rare circumstances the call to Response.End() actually doesn't call Thread.Abort(), but instead calls HttpApplication.CompleteRequest().

当您调用Response.Redirect(url)时抛出ThreadAbortException,因为系统在将重定向发送到响应流后中止当前网页线程的处理。 Response.Redirect(url)实际上是在内部调用Response.End(),而它的Response.End()调用Thread.Abort(),它会使堆栈冒泡以结束线程。在极少数情况下,对Response.End()的调用实际上不会调用Thread.Abort(),而是调用HttpApplication.CompleteRequest()。

Or simply move Response.Redirect("~/Membership/UserRegistration.aspx"); out of the Try/Catch block.

或者只需移动Response.Redirect(“〜/ Membership / UserRegistration.aspx”);超出Try / Catch块。

#3


2  

you can change like this Response.Redirect ("Login.aspx",false) then it wont abort.

你可以改变这样的Response.Redirect(“Login.aspx”,false)然后它不会中止。

#4


1  

For all caught errors where you want to redirect, create a 'GoTo' destined out of the Try Catch as follows:

对于要重定向的所有捕获错误,请创建一个从Try Catch注定的“GoTo”,如下所示:

    Try 

       'do stuff

    Catch ex As Exception

        'logging
        GoTo MyRedirection

    End Try

    'Prevent redirection in case of no errors
    Exit Sub

MyRedirection:
    Response.Redirect("login.aspx", True)

This neither causes thread abortion nor requires multiple catches.

这既不会导致线程中止,也不需要多次捕获。