![](/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.