【问题标题】:Response.End() Error [duplicate]Response.End() 错误 [重复]
【发布时间】:2012-09-13 04:39:00
【问题描述】:

我一直在尝试使用 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.}

我用谷歌搜索了这个,从我看到的有两种解决方案,一种是使用HttpContext.Current.ApplicationInstance.CompleteRequest();,另一种是捕获异常而不做任何事情。

首先,'catch and do nothing' 似乎是一种非常“糟糕”的做法,当然我们应该实际处理异常或做一些事情以使异常不会发生?这肯定是好习惯吗?我不希望我的代码被大量的 try-catch 填充。

另一种方法HttpContext.Current.ApplicationInstance.CompleteRequest(); 似乎不起作用。

这是我的代码的 sn-p:

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

我想要做的就是输出文本'Blocked 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>

【问题讨论】:

    标签: c# asp.net html thread-safety


    【解决方案1】:

    这是有意的。它旨在抛出此异常,以便 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.NET 调用Page_Load 以及页面生命周期中的其余事件。

    【讨论】:

    • 啊,好吧,那是有道理的。谢谢。
    猜你喜欢
    • 2012-05-23
    • 2013-03-21
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多