Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Response.End()错误_C#_Asp.net_Html_Thread Safety - Fatal编程技术网

C# Response.End()错误

C# Response.End()错误,c#,asp.net,html,thread-safety,C#,Asp.net,Html,Thread Safety,我一直在尝试使用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.Applicati

我一直在尝试使用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()
首先,“捕获而不做”似乎是一种非常“糟糕”的方法,我们确实应该处理异常或者做一些事情以避免异常发生?这当然是个好习惯吗?我不希望我的代码中充满大量的尝试捕获

另一个方法是
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;
我所要做的就是输出文本“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>
被阻止的IP

这是有意的。它旨在引发此异常,以便asp.net知道如何停止当前执行


Page\u Init
中调用
response.End()
时,它不允许ASP.NET调用
Page\u加载
和页面生命周期中的其他事件。

啊,好的,这是有意义的。谢谢
Calls to the End, Redirect, and Transfer methods throw a ThreadAbortException exception when the current response ends prematurely.