How to Avoid Response.End() “Thread was being aborted” Exception during the Excel file download

后端 未结 16 2245
感动是毒
感动是毒 2020-11-27 11:47

I tried to convert my dataset into excel and download that excel .I got my required excel file.But System.Threading.ThreadAbortException was raised every excel download. Ho

16条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 12:07

    Use a special catch block for the exception of the Response.End() method

    {
        ...
        context.Response.End(); //always throws an exception
    
    }
    catch (ThreadAbortException e)
    {
        //this is special for the Response.end exception
    }
    catch (Exception e)
    {
         context.Response.ContentType = "text/plain";
         context.Response.Write(e.Message);
    }
    

    Or just remove the Response.End() if your building a filehandler

提交回复
热议问题