asp.net – Context.Response.End()和Thread正在中止

我正在使用Context.Response.End来关闭响应,但会收到错误“Thread is beingorted”。

如何正确关闭响应而不触发异常?

try {
   Context.Response.Clear();
   Context.Response.ContentType = "text/html"; 
   //Context.Response.ContentType = "application/json";
   JsonObjectCollection collection = new JsonObjectCollection();
   collection.Add(new JsonNumericValue("resultcode",1));
   collection.Add(new JsonStringValue("sourceurl",exchangeData.cUrl));
   collection.Add(new JsonStringValue("filename",fileName));
   collection.Add(new JsonStringValue("filesize",fileSize));
   collection.Add(new JsonStringValue("fileurl",Common.GetPDFURL + outputFileName));
   JsonUtility.GenerateIndentedJsonText = true;
   Context.Response.Write(collection);
  try {
     Context.Response.End();
  } catch (ThreadAbortException exc) {
     // This should be first catch block i.e. before generic Exception
     // This Catch block is to absorb exception thrown by Response.End
  }
} catch (Exception err) {

}

自己解决代码应该是这样的

try {
  Context.Response.End();
} catch (ThreadAbortException err) {

}
catch (Exception err) {
}

解决方法

有没有特定的原因你不使用context.ApplicationInstance.CompleteRequest()而不是?

这种方法会使ASP.NET管道(EndRequest事件除外)导致短路,而不会抛出ThreadAbortException,因此您不需要额外的try / catch块,您也将体验更好的性能

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...