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

发布于 2024-10-06 03:51:43 字数 361 浏览 5 评论 0原文

今天下午,我的错误日志中出现了此错误线程正在中止。

导致此错误的代码是:

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

如果我将 bool 值更改为 false,错误日志将变为空,并且该错误将不再出现,但程序将停止工作。

如果我保持原样,我会收到这个错误,就像令人讨厌的一样。

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可是我不能没有你 2024-10-13 03:51:43

我捕获此异常并将其吞下,因为 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.
}

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

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.
}
肥爪爪 2024-10-13 03:51:43

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 块即可。

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

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().

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

一生独一 2024-10-13 03:51:43

你可以这样改变
Response.Redirect(“登录.aspx”,假)
那么它就不会中止。

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

迷途知返 2024-10-13 03:51:43

对于要重定向的所有捕获的错误,请创建一个指向 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)

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

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

    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文