C# 使用 Response.Redirect() 时获取线程中止异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18465334/
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
Getting Thread Abort Exception while using Response.Redirect()
提问by Sai Avinash
I wrote the following piece of code in a page which is under Update Panel.
我在更新面板下的页面中编写了以下代码。
protected void myGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName =="EditNames")
{
long lSelectedName = Convert.ToInt64(e.CommandArgument);
Session["SelectedItem"] = lSelectedName;
Response.Redirect("EditItem.aspx");
}
else if (e.CommandName =="DeleteNames")
{
long lSelectedName = Convert.ToInt64(e.CommandArgument);
ValidName.DeleteItem(lSelectedName);
ScriptManager.RegisterStartupScript(this, GetType(), "Key", "alert('Name deleted sucessfully')", true);
}
}
catch (System.Threading.ThreadAbortException)
{
}
catch (Exception ex)
{
Error handling code...
}
}
Here, I am getting a Thread Abort Exception while redirecting. However, I resolved it by using an error handler System.Threading.ThreadAbortException
.
在这里,我在重定向时收到线程中止异常。但是,我通过使用错误处理程序解决了它System.Threading.ThreadAbortException
。
But I am unsure why that error came while redirecting. Even though I solved this problem, I would like to know is there any mistake in the way I am coding or is there any way to stop the error firing at all.
但我不确定为什么在重定向时会出现该错误。即使我解决了这个问题,我也想知道我编码的方式是否有任何错误,或者有什么方法可以阻止错误触发。
Give your inputs...
提供您的意见...
Note that the page is under AJAX UPDATE PANEL.
请注意,该页面位于 AJAX 更新面板下。
采纳答案by Mike Perrenoud
Even though, i solved this problem , i would like to know is there any mistake in the way i am coding
尽管我解决了这个问题,但我想知道我的编码方式是否有任何错误
No mistake, you've done well.
没有错,你做得很好。
This error is expected. It's thrown because the server thread is in fact aborted when redirecting. From the MSDN documentation:
这个错误是意料之中的。它被抛出是因为服务器线程实际上在重定向时中止了。从MSDN 文档:
If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes.
如果为 endResponse 参数指定 true,则此方法将调用原始请求的 End 方法,该方法在完成时抛出 ThreadAbortException 异常。
and the documentation for the overload you're using:
Redirect calls End which throws a ThreadAbortException exception upon completion.
重定向调用 End ,它在完成时抛出 ThreadAbortException 异常。
回答by Moiz Tankiwala
Please read this article - http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx
Instead of ending the request, it is a good practice to bypass the request execution pipeline by calling the Context.ApplicationInstance.CompleteRequest()
.
最好不要结束请求,而是通过调用Context.ApplicationInstance.CompleteRequest()
.
So your code would look something like this:
所以你的代码看起来像这样:
Response.Redirect("TargetPage", false); //write redirect
Context.ApplicationInstance.CompleteRequest(); // end response
回答by user3098514
It is enough to mention false in the redirect method, like, Response.Redirect("TargetPage", false);
在重定向方法中提及 false 就足够了,例如 Response.Redirect("TargetPage", false);
回答by Mahdi Esmailoghli
This is happening because you are redirecting inside of your try/catch block. Don't do this.
发生这种情况是因为您在 try/catch 块内重定向。不要这样做。