错误:System.Threading.ThreadAbortException中的异常:线程正在中止

时间:2014-07-11 06:18:31

标签: c# asp.net multithreading

下载模板时,我收到以下错误消息。

我尝试过而不是Response.Flush();与Response.End();.但得到同样的错误。

Error: Excepiton in Download:System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort(Object stateInfo)
at System.Web.HttpResponse.End()

任何想法都要避免上述异常

代码

private void DownloadFile(string filePath, string downloadFileName)
{
    Response.ContentType = "application/ms-excel";
    Response.AddHeader("content-disposition", "attachment; filename=" + downloadFileName);
    Response.TransmitFile(filePath);
    // Response.Flush();
    Response.End();
}

提前致谢..

1 个答案:

答案 0 :(得分:5)

在这里回答: - How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file download

  

替换为:HttpContext.Current.Response.End();

     

有了这个:

HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
HttpContext.Current.Response.SuppressContent = true;  // Gets or sets a value indicating whether to send HTTP content to the client.
HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline**
  

执行链并直接执行EndRequest事件。

在这里回答: - ASP.NET exception "Thread was being aborted" causes method to exit

  

这是一个ThreadAbortException;这是一个特殊的例外   除非你,否则会在每个捕获块的末尾自动重新生成   调用Thread.ResetAbort()。

     

ASP .Net方法,如Response.End或Response.Redirect(除非你   传递false)抛出此异常以结束当前的处理   页;你的someFunctionCall()可能正在调用其中一个   方法

     

ASP .Net本身处理此异常并调用ResetAbort   继续处理。