Response.Redirect(string url)报错:System.Threading.ThreadAbortException: 正在中止线程。

今天用Response.Redirect(string url)重定向时,生产环境总是报ThreadAbortException异常,而调试时没问题。在网上查了很久,都是一样的解决方案,写的貌似很详细,实际不清不楚,解决不了问题。
实际上微软官方的文档已经给了解决方案,只是写的不是很详细,但确实解决了我的问题。

先看报错的代码:

Response.Redirect("xxx.aspx");

再看报错:

System.Threading.ThreadAbortException: 正在中止线程。
   在 System.Threading.Thread.AbortInternal()
   在 System.Threading.Thread.Abort(Object stateInfo)
   在 System.Web.HttpResponse.AbortCurrentThread()
   在 System.Web.HttpResponse.End()
   在 System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent)
   在 System.Web.HttpResponse.Redirect(String url)
   在 WebYKT.Web.DingTalkLogin.Page_Load(Object sender, EventArgs e) 位置 E:\xxx.aspx.cs:行号 231

然后看下微软的文档怎么说的:
Response.Redirect
这里有2点需要注意:

  1. 使用 HttpResponse.Redirect(String, Boolean) 重载并传递 false endResponse 参数;
  2. 调用 CompleteRequest 方法;

修改后的最终代码:

Response.Redirect("http://xxx.xxx.xxx.xxx/xxx.aspx", false); //这里补全了ip/域名,如果没有补全,用于移动页面时该页可能会丢失。
Context.ApplicationInstance.CompleteRequest();

猜你喜欢

转载自blog.csdn.net/danding_ge/article/details/109243393