2017-02-20 22 views
0

ASP.NET WebフォームのボタンのOnClickメソッドでは、Response.Redirect()これは、エラーメッセージを表示してスレッドを中止するようにシステムを発生しますResponse.Redirect()を使用すると「例外がスローされました: 'System.Threading.ThreadAbortException' in mscorlib.dll」

Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll 

ここで次のようないくつかの質問には、自社のソリューションを使用して、あります私は変更:

Response.Redirect("~/UI/Home.aspx"); 

Response.Redirect("~/UI/Home.aspx", false); 
Context.ApplicationInstance.CompleteRequest(); 
01に

しかし、私はまだ同じ問題を抱えています。デバッガを使用してコードを実行し、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."; 
     } 
    } 

任意の考え?

+1

'のResponse.Redirect()' false'を引数...の – David

+0

が重複する可能性が与えられたとき、 '*本当に*その例外をスローすべきではない[Response.RedirectをはSystem.Threading.ThreadAbortExceptionの原因となるのはなぜ?](http://stackoverflow.com/questions/2777105/why-response-redirect-causes-system-threading-threadabortexception) –

+0

@Am_I_Helpful非常によく似た問題ですが、解決方法で解決されていません。 –

答えて

1

私は過去にこの問題を見てきました。あなたはこのコードを使用する場合は理論的には、起こるべきではありません。

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. 
    } 
} 
+0

答えをありがとう!残念ながら、これは問題を解決するようではありません - まだログインページをリロードしています。 –

0

これは、セッションが対象ページ内の特定の要素が含まれていなかった場合、私は戻ってリダイレクトすることによって引き起こされた問題であることが判明し、この例では、それはしませんでした!例外はスローされますが、目に見える問題は発生しません。

おかげ

関連する問題