2014-10-27 50 views
2

在執行線ThreadAbortException在asp.net

Server.Transfer("Payment.aspx?vpc_ChannelId=2", true); 

,從而指出了這個答案https://stackoverflow.com/a/1252119/1169180 & https://stackoverflow.com/a/11130517/1169180

我改變了我的代碼

protected void Page_Load(object sender, EventArgs e) 
{ 
    try 
    { 
    UserContext conObj = new UserContext(); 
    HttpContext CurrContext = HttpContext.Current; 
    if (!IsPostBack) 
    { 
     // Code 
    } 
    else 
    { 
     string userContext = hdnContextObj.Value; 
     conObj = JsonConvert.DeserializeObject<UserContext>(userContext); 
     CurrContext.Items.Add("Context", conObj); 
     try 
     { 
     Server.Transfer("Payment.aspx?vpc_ChannelId=2", true); 
     } 
     catch (ThreadAbortException xObj) 
     { 
     } 
     finally 
     { 
     Server.Transfer("Payment.aspx?vpc_ChannelId=2", true); 
     } 
    } 
    } 
    catch (Exception xObj) 
    { 
    Response.Write("Exception : " + xObj.Message); 
    } 
} 
我收到錯誤 Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.服務器轉移

我仍然收到同樣的例外,在捕捉塊

也如這裏指出的http://support.microsoft.com/kb/312629/EN-US/我用Server.Execute但它沒有重定向到Payment.aspx頁,而只是刷新。

+0

你的try/catch是什麼都不做,你正趕上,然後reshowing相同的異常,這是一樣的根本沒有抓住它。如果你想吞下異常,那麼不要扔進你的catch塊。 – 2014-10-27 09:49:41

+0

@BenRobinson:即使我刪除catch塊的投擲。當行終於得到執行,異常獲得最外面的catch塊 – Shaggy 2014-10-27 10:08:06

回答

4

因爲運行操作的線程被迫在多個終止的異常引發稱爲由於轉移而導致的地點。因此,可以安全地忽略這個例外,因爲你的鏈接的答案表明。

您可以通過捕獲異常並不拋出它來忽略異常。

try 
{ 
    Server.Transfer("Payment.aspx?vpc_ChannelId=2", true); 
} 
catch(ThreadAbortException) 
{ 
    // Exception ignored: Thread Abort = discontinue processing on the current page 
} 

另外,作爲MSDN文章建議,你可以使用Server.Execute代替。

要解決此問題,使用下列方法之一:

  • 對於Response.End,調用而不是Response.EndHttpContext.Current.ApplicationInstance.CompleteRequest方法繞過代碼執行到Application_EndRequest事件。

  • 對於Response.Redirect,使用的過載,Response.Redirect(String url, bool endResponse),對於endResponse參數來抑制內部呼叫到Response.End傳遞false。例如:

    Response.Redirect ("nextpage.aspx", false); 
    

    如果使用此替代方法,將執行Response.Redirect之後的代碼。

  • 對於Server.Transfer,請改爲使用Server.Execute方法。

//澄清Server.Execute

MSDN DOC澄清的Server.Execute用法。記住這不是重定向很重要,它像一個函數調用一樣。所以在調用之後的任何代碼也會被執行。如果您不希望代碼執行,則可以使用returnResponse.End

在OP的例子,在使用自己的代碼可能是這個樣子Server.Execute

protected void Page_Load(object sender, EventArgs e) 
{ 
    try 
    { 
     UserContext conObj = new UserContext(); 
     HttpContext CurrContext = HttpContext.Current; 
     if (!IsPostBack) 
     { 
      // Code 
     } 
     else 
     { 
      string userContext = hdnContextObj.Value; 
      conObj = JsonConvert.DeserializeObject<UserContext>(userContext); 
      CurrContext.Items.Add("Context", conObj); 
      Server.Execute("Payment.aspx?vpc_ChannelId=2", true); 
      Response.End(); // or return; 
     } 
    } 
    catch (Exception xObj) 
    { 
     Response.Write("Exception : " + xObj.Message); 
    } 
} 
+0

我試着你的解決方案建議'Server.Execute'不重定向我的頁面&當finally塊得到執行它進一步拋出異常 – Shaggy 2014-10-27 10:13:51

+0

@Shaggy我已經添加了一個例子。記住,'Server.Execute'就像一個函數調用一樣,它會將控制權返回給正在執行的應用程序。所以,你需要手動結束響應或者跳出一個程序。 – Kami 2014-10-27 10:24:59

+1

現在在'Response.End()'它使用'Server.Execute'後出現同樣的錯誤 – Shaggy 2014-10-27 10:28:45

0

Server.Transfer(「Payment.aspx?vpc_ChannelId = 2」,false);

這將適用於你。它會在傳輸代碼後停止其餘代碼,以便它不會執行該代碼。

+0

我想保存表單數據。所以真實是必需的。 – Shaggy 2014-10-27 09:50:40

+0

好的,但我們可以看到其餘的代碼?因爲這很可能是導致此問題的原因 – Koen 2014-10-27 10:05:49

+0

檢查問題編輯 – Shaggy 2014-10-27 10:12:32

0

ThreadAbortException異常造成的,因爲都到Response.End在Server.Redirect和Server.Transfer的internally.Try像這樣

Response.Write("<script language=\"javascript\" type=\"text/javascript\">window.location.href = 'Your.aspx'; </script>"); 
+0

我想保留'context'對象。所以我想要的server.transfer – Shaggy 2014-10-27 10:11:21

相關問題