gpt4 book ai didi

c# - asp.net 中服务器传输中的 ThreadAbortException

转载 作者:行者123 更新时间:2023-11-30 20:46:40 25 4
gpt4 key购买 nike

我收到错误 Unable to evaluate expression because the code is optimized or a native frame is on the top of the call stack. while executing line

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);
}
}

我仍然在 out catch block 中遇到相同的异常

也正如这里指出的那样http://support.microsoft.com/kb/312629/EN-US/我使用了 Server.Execute 但它没有重定向到 Payment.aspx 页面,而是只是刷新。

最佳答案

引发异常是因为运行该操作的线程由于传输而被迫在多个位置终止。因此,按照链接答案的建议忽略此异常是安全的。

您可以通过捕获异常而不抛出异常来忽略异常。

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

或者,作为 MSDN文章建议,您可以改用 Server.Execute

To work around this problem, use one of the following methods:

  • For Response.End, call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_EndRequest event.

  • For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that passes false for the endResponse parameter to suppress the internal call to Response.End. For example:

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

    If you use this workaround, the code that follows Response.Redirect is executed.

  • For Server.Transfer, use the Server.Execute method instead.

//关于Server.Execute 的说明

MSDN文档阐明了 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);
}
}

关于c# - asp.net 中服务器传输中的 ThreadAbortException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26584650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com