![](/img/trans.png)
[英]System.Threading.ThreadAbortException occurred in mscorlib.dll occuring persitently
[英]“Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll” when using Response.Redirect()
在ASP.NET Web表单中按钮的OnClick方法中,我对Response.Redirect()进行了调用,这导致系统中止带有错误消息的线程:
Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll
这里有一些类似的问题,使用我更改的解决方案:
Response.Redirect("~/UI/Home.aspx");
至
Response.Redirect("~/UI/Home.aspx", false);
Context.ApplicationInstance.CompleteRequest();
但是我仍然遇到同样的问题。 使用调试器,我遍历了代码,所有代码均成功执行,直到调用了Response.Redirect();。
OnClick功能
protected void btnLogin_Click(object sender, EventArgs e)
{
SiteUser s = null;
try
{
string email = txtEmail.Text;
string pwd = txtPwd.Text;
s = DBConnection.login(email, pwd);
}
catch (Exception ex)
{
Console.Write(ex);
lblLoginError.Text = "Error logging in.";
}
if (s != null)
{
Session["UserSession"] = s;
Response.Redirect("~/UI/Home.aspx", false);
Context.ApplicationInstance.CompleteRequest();
}
else
{
lblLoginError.Text = "User not found. Please check your details and try again.";
}
}
对为什么会发生这种情况有任何想法吗?
我过去曾见过这个问题。 从理论上讲,如果使用此代码,则不应发生:
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();
话虽如此,我有时还是得到这些,这真是令人惊讶。 我猜它有时会在活动的finally
块存在的情况下发生,以向代码发出信号以开始清理自身,尽管您似乎并非如此。
我能想到的最好的解决方法是捕获错误并忽略它。
protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
SiteUser s = null;
try
{
string email = txtEmail.Text;
string pwd = txtPwd.Text;
s = DBConnection.login(email, pwd);
}
catch (Exception ex)
{
Console.Write(ex);
lblLoginError.Text = "Error logging in.";
}
if (s != null)
{
Session["UserSession"] = s;
Response.Redirect("~/UI/Home.aspx", false);
Context.ApplicationInstance.CompleteRequest();
}
else
{
lblLoginError.Text = "User not found. Please check your details and try again.";
}
}
catch(System.Threading.ThreadAbortException)
{
//Do nothing. The exception will get rethrown by the framework when this block terminates.
}
}
如果会话在目标页面中不包含特定元素,而事实并非如此,这是我通过重定向回引起的一个问题! 仍然会引发异常,但不再导致可见的问题。
谢谢
我解决这个
Response.Redirect(.....)
HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.