当我使用Response.Redirect(…)将我的表单重定向到一个新页面时,我得到了错误:
类型为'System.Threading '的第一次机会异常。mscorlib.dll中出现ThreadAbortException `
System.Threading类型的异常。在mscorlib.dll中出现ThreadAbortException',但在用户代码中没有处理
我对此的理解是,该错误是由web服务器中止余下的页面响应引起的。重定向被调用。
I know I can add a second parameter to Response.Redirect that is called endResponse. If I set endResponse to True I still get the error but if I set it to False then I do not. I am pretty sure though that that means the webserver is running the rest of the page I redirected away from. Which would seem to be inefficient to say the least. Is there a better way to do this? Something other than Response.Redirect or is there a way to force the old page to stop loading where I will not get a ThreadAbortException?
redirect()抛出异常终止当前请求。
这篇KB文章描述了这种行为(也适用于Request.End()和Server.Transfer()方法)。
对于Response.Redirect()存在重载:
Response.Redirect(String url, bool endResponse)
如果传递endResponse=false,则不会抛出异常(但运行时将继续处理当前请求)。
如果endResponse=true(或者使用了其他重载),则抛出异常,当前请求将立即终止。
我也尝试了其他解决方案,但一些代码执行后重定向。
public static void ResponseRedirect(HttpResponse iResponse, string iUrl)
{
ResponseRedirect(iResponse, iUrl, HttpContext.Current);
}
public static void ResponseRedirect(HttpResponse iResponse, string iUrl, HttpContext iContext)
{
iResponse.Redirect(iUrl, false);
iContext.ApplicationInstance.CompleteRequest();
iResponse.BufferOutput = true;
iResponse.Flush();
iResponse.Close();
}
所以如果需要防止代码执行后重定向
try
{
//other code
Response.Redirect("")
// code not to be executed
}
catch(ThreadAbortException){}//do there id nothing here
catch(Exception ex)
{
//Logging
}