C# 错误:无法计算表达式,因为代码已优化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10982949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:01:39  来源:igfitidea点击:

Error: Unable to evaluate expression because the code is optimized

c#asp.netmaster-pagesserver.transfer

提问by DNR

I am getting an error in my asp.net app that reads

我在我的 asp.net 应用程序中收到一个错误,内容为

"Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."

“无法评估表达式,因为代码已优化或本机框架位于调用堆栈的顶部。”

protected void btnCustomerProfile_Click(object sender, EventArgs e)
{
    try
    {
        Server.Transfer("CustomerProfile.aspx");
    }
    catch (Exception ex)
    {
        Response.Write(ex.ToString());
    }
    finally
    { }
}

After searching SO, I see most of the similar posts involve response.redirect. My code is using server.transfer and my application is also using Master Pages.

搜索 SO 后,我看到大多数类似的帖子都涉及 response.redirect。我的代码使用 server.transfer,我的应用程序也使用母版页。

How can I resolve this issue?

我该如何解决这个问题?

Update:For some reason, this error occurs is I use Response.Redirect as well. Unfortunately I cannot use Server.Execute, because Server.Execute calls the calling page towards the end.

更新:出于某种原因,发生此错误的原因是我也使用了 Response.Redirect。不幸的是,我不能使用 Server.Execute,因为 Server.Execute 会在最后调用调用页面。

采纳答案by Troy

You will get an error, but the code block below will trap it and you can get on with your life.

你会得到一个错误,但下面的代码块会捕获它,你可以继续你的生活。

Try this:

尝试这个:

using System.Threading.ThreadAbortException;

catch(ThreadAbortException ex)
{
    throw;
}

回答by AvengingBudda

The issue you describe seems to be by design as shown here:

您描述的问题似乎是设计使然,如下所示:

http://support.microsoft.com/kb/312629/EN-US/

http://support.microsoft.com/kb/312629/EN-US/

Using Server.Executeshould solve the problem

使用Server.Execute应该可以解决问题

回答by christiandev

Have you tried replacing the server.transferwith response.redirect()?

您是否尝试过更换server.transferresponse.redirect()

Server.Transfer VS Response.Redirect

Server.Transfer VS Response.Redirect

回答by jit4peace

I faced this message when I was testing working of multi-threading application using MS-Test.

当我使用 MS-Test 测试多线程应用程序的工作时,我遇到了这条消息。

I found the reason for this was because the testing main thread got ended and initialized cleaning of objects while other created threads which are meant to run infinitely were still working.

我发现这是因为测试主线程结束并初始化了对象的清理,而其他创建的旨在无限运行的线程仍在工作。

As teat clean up method kills objects these threads gets aborted showing above message.

当奶头清理方法杀死对象时,这些线程被中止,显示上述消息。