这是我的代码:
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!";
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,即使它在catch之后重定向.
这是错误:
"System.Threading.ThreadAbortException:线程正在中止.\ r \n在System.Threading.Thread.AbortInternal()\ r \n中System.Threading.Thread.Abort(对象stateInfo)\ r \n在System.Web上.HttpResponse.End()\ r \n在System.Web.HttpResponse.Redirect(String url,Boolean endResponse)\ r \n在System.Web.HttpResponse.Redirect(String url)\ r \n
谁能告诉我为什么会出现这个错误?
Cod*_*ick 19
你可以简单地移动....
Response.Redirect("~/Membership/UserRegistration.aspx");
Run Code Online (Sandbox Code Playgroud)
...在Try/Catch块之外,您可以尝试下面的John S. Reid的新解决方案:
Response.Redirect(url)ThreadAbortException解决方案
作者:John S. Reid
2004年3月31日
(2006年10月28日编辑,包含更多细节并修复了我的分析中的一些不准确之处,尽管其核心的解决方案仍然相同)
......跳过......
当你做出的Response.Redirect(URL)的调用,因为系统会中止当前网页线程处理发送重定向响应流后ThreadAbortException异常.的Response.Redirect(网址),实际上使()在内部调用到Response.End,它的到Response.End()调用Thread.Abort的()的气泡的堆栈结束线程.在极少数情况下,对Response.End()的调用实际上不会调用Thread.Abort(),而是调用HttpApplication.CompleteRequest().(有关详细信息和解决方案的提示,请参阅此Microsoft支持文章.)
......跳过......
PostBack和渲染解决方案?覆盖.
我们的想法是创建一个类级变量,标记页面是否应该终止,然后在处理事件或呈现页面之前检查变量.应在调用HttpApplication.CompleteRequest()之后设置此标志.您可以将您在每一个回发事件或渲染块这个值,但可以是乏味且容易出错,所以我会建议只是重写RaisePostBackEvent和渲染方法的代码示例1所示:
Run Code Online (Sandbox Code Playgroud)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); }最终分析
最初,我曾建议你应该只需更换所有来电到的Response.Redirect(URL)与的Response.Redirect(网址,FALSE)和CompleteRequest()调用,但如果你想避免回发处理和HTML渲染你会还需要添加覆盖.从我的代码,我可以看到,最有效的方式来重定向和最终处理的深度分析,近期是使用Response.Redirect(URL)方法,让线程中止一路向上堆栈,但是如果这个例外正如在许多情况下那样让你感到悲痛,那么这里的解决方案是下一个最好的事情.
还应该注意,Server.Transfer()方法遇到了同样的问题,因为它在内部调用了Response.End().好消息是它可以通过使用上面的解决方案以相同的方式解决,并用Server.Execute()替换对Response.Redirect()的调用.
1 - 我修改了代码格式,使其适合SO边界,因此不会滚动.
| 归档时间: |
|
| 查看次数: |
10538 次 |
| 最近记录: |