Response.Redirect()
出现问题并得到错误:
无法计算表达式,因为代码已优化或本机帧位于调用堆栈顶部
我在谷歌上找到了一些主题,比如:无法计算表达式,因为代码已优化或本机帧位于调用堆栈的顶部
人们提供将第二个参数设置为false
值以使其工作,如:
Response.Redirect("PageName.aspx", false);
但是对于我的程序,它没有重定向到新的…它只是继续停留在这个页面。
我在另一个页面上做了一个断点,我想重定向到它。但是断点没有捕获任何事件。看起来它只是不发送请求到另一个页面。
我的代码相当大,所以我没有在这里发布代码,但在ideone:http://ideone.com/bQzCJd
Redirect()
方法在57th line
上,其:
- 发生异常,如果第二个参数被设置为
true
- 设置为
false
时,无任何反应
我必须做什么来修复它?
发生异常,如果第二个参数被设置为true
是的,ThreadAbortException
,因为你在Try-Catch
中重定向。
为什么不在try/catch
之后使用Response.Redirect("PageName.aspx")
呢?使用bool
变量,例如redirectCabinet
,您可以在重定向前检查:
bool redirectCabinet = false;
try
{
// ...
if (tmpStr != String.Empty)
{
if (pageHandler.Request.HttpMethod == "GET")
{
if (pageHandler.Request.Params["method"] == "checkAuth")
{
// ...
bool userExists = Convert.ToBoolean(mysqlObject.MakeScalar(ref mysqlConn,
if (userExists)
{
// ...
redirectCabinet = true;
//Response.Redirect("Cabinet.aspx");
}
// ...
}
}
//....
}
} catch (Exception exc)
{
exc.ToString();
}
if(redirectCabinet)
Response.Redirect("Cabinet.aspx");
我建议您避免Abort Exception,并使用true结束执行并移动到下一页。
try
{
// this is throw the ThreadAbortException exception
Response.Redirect("SecondEndPage.aspx", true);
}
catch (ThreadAbortException)
{
// ignore it because we know that come from the redirect
}
catch (Exception x)
{
}
类似:https://stackoverflow.com/a/14641145/159270
即使在评论中提出的解决方案,它也不能正常工作,但如果用真正的endRequest
重定向,它应该可以工作。
:
if(redirectCabinet)
Response.Redirect("Cabinet.aspx",true);