简体   繁体   English

ASP.NET Response.Redirect() 错误

[英]ASP.NET Response.Redirect( ) Error

Here is my code:这是我的代码:

try
{
    Session["CuponeNO"] = txtCode.Text;
    txtCode.Text = string.Empty;
    Response.Redirect("~/Membership/UserRegistration.aspx");
}
catch(Exception ex)
{
   string s = ex.ToString();
   lblMessage1.Text = "Error Occured!";
}

I am getting an error, even though it redirects after catch.我收到一个错误,即使它在捕获后重定向。

Here is the error:这是错误:

"System.Threading.ThreadAbortException: Thread was being aborted.\r\n at System.Threading.Thread.AbortInternal()\r\n at System.Threading.Thread.Abort(Object stateInfo)\r\n at System.Web.HttpResponse.End()\r\n at System.Web.HttpResponse.Redirect(String url, Boolean endResponse)\r\n at System.Web.HttpResponse.Redirect(String url)\r\n "System.Threading.ThreadAbortException: 线程被中止。\r\n 在 System.Threading.Thread.AbortInternal()\r\n 在 System.Threading.Thread.Abort(Object stateInfo)\r\n 在 System.Web .HttpResponse.End()\r\n at System.Web.HttpResponse.Redirect(String url, Boolean endResponse)\r\n at System.Web.HttpResponse.Redirect(String url)\r\n

Can anyone tell me why this error is occurring?谁能告诉我为什么会发生这个错误?

You could simply move....你可以简单地移动......

Response.Redirect("~/Membership/UserRegistration.aspx");

... outside of the Try / Catch block or you can try John S. Reid's newer solution below: ...在 Try / Catch 块之外,或者您可以尝试以下John S. Reid 的更新解决方案

Response.Redirect(url) ThreadAbortException Solution Response.Redirect(url) ThreadAbortException 解决方案


by John S. Reid约翰·S·里德
March 31, 2004 2004 年 3 月 31 日
(edited October 28, 2006 to include greater detail and fix some inaccuracies in my analysis, though the solution at it's core remains the same) (2006 年 10 月 28 日编辑,在我的分析中包含更多细节并修复了一些不准确之处,尽管其核心解决方案保持不变)

... skipping down... ……跳下去……

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) 时会引发 ThreadAbortException,因为系统在将重定向发送到响应 stream 后会中止当前 web 页面线程的处理。 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. Response.Redirect(url) 实际上在内部调用了 Response.End() ,而 Response.End() 调用了 Thread.Abort() ,它使堆栈冒泡以结束线程。 Under rare circumstances the call to Response.End() actually doesn't call Thread.Abort(), but instead calls HttpApplication.CompleteRequest().在极少数情况下,对 Response.End() 的调用实际上不会调用 Thread.Abort(),而是调用 HttpApplication.CompleteRequest()。 (See this Microsoft Support article for details and a hint at the solution.) (有关详细信息和解决方案的提示,请参阅此Microsoft 支持文章。)

... skipping down... ……跳下去……

PostBack and Render Solutions?回发和渲染解决方案? Overrides.覆盖。

The idea is to create a class level variable that flags if the Page should terminate and then check the variable prior to processing your events or rendering your page.这个想法是创建一个 class 级别的变量来标记页面是否应该终止,然后在处理您的事件或呈现您的页面之前检查该变量。 This flag should be set after the call to HttpApplication.CompleteRequest().此标志应在调用 HttpApplication.CompleteRequest() 之后设置。 You can place the check for this value in every PostBack event or rendering block but that can be tedious and prone to errors, so I would recommend just overriding the RaisePostBackEvent and Render methods as in the code sample 1 below:您可以在每个 PostBack 事件或渲染块中检查此值,但这可能很乏味且容易出错,因此我建议您只覆盖 RaisePostBackEvent 和 Render 方法,如下面的代码示例1所示:

 private bool m_bIsTerminating = false; protected void Page_Load(object sender, EventArgs e) { if (WeNeedToRedirect == true) { Response.Redirect(url, false); HttpContext.Current.ApplicationInstance.CompleteRequest(); m_bIsTerminating = true; // Remember to end the method here if there is more code in it. return; } } protected override void RaisePostBackEvent ( IPostBackEventHandler sourceControl, string eventArgument ) { if (m_bIsTerminating == false) base.RaisePostBackEvent(sourceControl, eventArgument); } protected override void Render(HtmlTextWriter writer) { if (m_bIsTerminating == false) base.Render(writer); }

The Final Analysis最终分析

Initially I had recommended that you should simply replace all of your calls to Response.Redirect(url) with the Response.Redirect(url, false) and CompleteRequest() calls, but if you want to avoid postback processing and html rendering you'll need to add the overrides as well.最初我建议您应该简单地将所有对 Response.Redirect(url) 的调用替换为 Response.Redirect(url, false) 和 CompleteRequest() 调用,但是如果您想避免回发处理和 html 渲染,您将还需要添加覆盖。 From my recent in depth analysis of the code I can see that the most efficient way to redirect and end processing is to use the Response.Redirect(url) method and let the thread be aborted all the way up the stack, but if this exception is causing you grief as it does in many circumstances then the solution here is the next best thing.从我最近对代码的深入分析中,我可以看到重定向和结束处理的最有效方法是使用 Response.Redirect(url) 方法并让线程一直中止堆栈,但如果出现此异常就像在许多情况下一样让您感到悲伤,那么这里的解决方案是下一个最好的事情。

It should also be noted that the Server.Transfer() method suffers from the same issue since it calls Response.End() internally.还应该注意的是,Server.Transfer() 方法也存在同样的问题,因为它在内部调用了 Response.End()。 The good news is that it can be solved in the same way by using the solution above and replacing the call to Response.Redirect() with Server.Execute().好消息是,可以通过使用上述解决方案并将对 Response.Redirect() 的调用替换为 Server.Execute() 以相同的方式解决。

1 - I modified the code formatting to make it fit inside SO boundaries so it wouldn't scroll. 1 - 我修改了代码格式以使其适合 SO 边界内,因此它不会滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM