Response.Redirect()ThreadAbortException间歇性地冒泡
我了解(现在),Response.Redirect()和Response.End()抛出ThreadAbortException是杀死当前处理线程以模仿ASP Classic的Response.End()和Response.Redirect方法的行为的昂贵方式
I understand (now) that Response.Redirect() and Response.End() throw a ThreadAbortException as an expensive way of killing the current processing thread to emulate the behaviour of ASP Classic's Response.End() and Response.Redirect methods.
但是。
在我们的应用程序中,间歇性地出现了异常气泡。例如,我们有一个从客户端javascript调用的页面,该页面返回一个小字符串以显示在页面中。
It seems intermittently in our application that the exception bubbles too high. For example, we have a page that is called from client side javascript to return a small string to display in a page.
protected void Page_Load(object sender, EventArgs e)
{
// Work out some stuff.
Response.Write(stuff);
Response.End();
}
这通常可行,但有时会在UI上冒泡
This generally works, but sometimes, we get the exception bubbling up to the UI layer and get part of the exception text displayed in the page.
类似地,在其他地方,我们有:
Similarly, else where we have:
// check the login is still valid:
if(!loggedin) {
Response.Redirect("login.aspx");
}
在某些情况下,用户被重定向到login.aspx,在其他情况下,用户将获得一个ASP.NET错误页面和堆栈转储(由于我们的开发服务器的配置方式)。
In some cases, the user is redirected to login.aspx, in others, the user gets an ASP.NET error page and stack dump (because of how our dev servers are configured).
ie在某些情况下,response.redirect会在执行重定向的过程中一直引发异常。为什么?我们如何阻止它?
i.e. in some cases, response.redirect throws an exception all the way up INSTEAD of doing a redirect. Why? How do we stop this?
您可以改用以下最佳实践代码,此答案所解释的是为了防止异常首先发生:
You can use the following best-practice code instead, as explained by this answer to prevent the exception from happening in the first place:
Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();