今天在家闲着无事,想找个时间补一下.net方面的基础知识.于是乎打了很久之前的一个小小的测试项目,编译项目成功.但是运行的时候居然报错.该项目前一阵子还在我公司的电脑上测试通过的.
出错代码部分:
1 2 3 4 5 6 7 8 9 10 11 12 | try { Response.Cookies["userId"].Value = u.userId.ToString(); Response.Cookies["username"].Value = u.username; Response.Redirect("Default.aspx"); Response.End(); } catch (Exception ex) { throw ex; } |
没办法,只有上去百度一下,发现还真有这个问题的解决方案.
如果使用 Response.End、Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常。您可以使用 try-catch 语句捕获此异常。
Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件。不执行 Response.End 后面的代码行。
此问题出现在 Response.Redirect 和 Server.Transfer 方法中,因为这两种方法均在内部调用 Response.End。
解决方案 :
要解决此问题,请使用下列方法之一:
• 对于 Response.End,调用 HttpContext.Current.ApplicationInstance.CompleteRequest 方法而不是 Response.End 以跳过 Application_EndRequest 事件的代码执行。
• 对于 Response.Redirect,请使用重载 Response.Redirect(String url, bool endResponse),该重载对 endResponse 参数传递 false 以取消对 Response.End 的内部调用。例如:
Response.Redirect (“Default.aspx”, false);
原文地址:http://www.cnblogs.com/zhangronghua/archive/2008/09/11/1289089.html
是线程的终止问题,我的解决方法是捕获(System.Threading.ThreadAbortException ee)
{
//do nothing 这里啥也不干,不过这样貌似性能上不是很好,你的方法很好
}
catch
{
throw;
}