2

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

私はこれをグーグルで検索しましたが、2つの解決策があります。1つは使用することHttpContext.Current.ApplicationInstance.CompleteRequest();で、もう1つは例外をキャッチして何もしないことです。

まず、「キャッチして何もしない」は、これを行うための非常に「悪い」方法のように見えます。確かに、実際に例外を処理するか、例外が発生しないように何かを行う必要がありますか?確かにそれは良い習慣ですか?コードをたくさんのトライキャッチで埋めたくありません。

他の方法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;

「BlockedIP」というテキストを出力するだけですが、この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>
4

1 に答える 1

3

これは意図されたものです。asp.netが現在の実行を停止することを認識できるように、この例外をスローするように設計されています。

HttpResponse.Endから。

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

Reponse.End()これは、たとえば、内で呼び出されPage_Init、ASP.NETPage_Loadやページライフサイクル内の残りのイベントを呼び出さない場合に使用することを目的としています。

于 2012-09-20T18:49:50.030 に答える