2012-09-20 46 views
2

我一直在嘗試使用Response.End()方法,並且一直在獲取此錯誤;Response.End()錯誤

[System.Threading.ThreadAbortException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.} 

我GOOGLE了這一點,從我所看到的有2個解決方案,一個是使用HttpContext.Current.ApplicationInstance.CompleteRequest();,另一個是剛剛捕獲異常,什麼也不做。

首先,「無所事事」似乎是一個非常「壞」的做法,我們當然應該實際處理異常或做一些事情,以避免異常發生?當然這是很好的做法?我不想讓我的代碼充滿大量的嘗試捕獲。

另一種方法HttpContext.Current.ApplicationInstance.CompleteRequest();似乎沒有工作。

這裏是我的代碼片段:

Response.Clear(); 
Response.ClearHeaders(); 
Response.AddHeader("Content-Type", "text/html"); 
Response.Write("Blocked IP"); 
Response.Flush(); 
HttpContext.Current.ApplicationInstance.CompleteRequest(); 
return; 

所有我想要做的是輸出「已屏蔽IP」,但與使用HttpContext.Current.ApplicationInstance.CompleteRequest();方法aspx頁面的其餘部分被下它印像文本這:

Blocked IP 

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head><title> 

    </title></head> 
    <body> 
     <form method="post" action="Email.aspx" id="form1"> 
    <div class="aspNetHidden"> 
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZIELtfylTwZMU8vGWe9UrhWClqN3i4rMTvAp8otze+6G" /> 
    </div> 

     <div> 

     </div> 
     </form> 
    </body> 
    </html> 

回答

3

這是打算。它旨在拋出此異常,以便asp.net知道停止當前執行。

HttpResponse.End

Calls to the End, Redirect, and Transfer methods throw a ThreadAbortException exception when the current response ends prematurely. 

據indended被用在可以說是Reponse.End()Page_Init調用,它不會讓ASP.NET呼叫Page_Load並在頁面生命週期事件的其餘部分。

+0

啊好吧,那種說法很有道理。謝謝。 –