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

后端 未结 16 2128
感动是毒
感动是毒 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:24

    Looks to be the same question as:

    When an ASP.NET System.Web.HttpResponse.End() is called, the current thread is aborted?

    So it's by design. You need to add a catch for that exception and gracefully "ignore" it.

    0 讨论(0)
  • 2020-11-27 12:25

    I recommend this solution :

    1. Don't use response.End();

    2. Declare this global var : bool isFileDownLoad;

    3. Just after your (response.Write(sw.ToString());) set ==> isFileDownLoad = true;

    4. Override your Render like :

      /// AEG : Very important to handle the thread aborted exception
      
      override protected void Render(HtmlTextWriter w)
      {
           if (!isFileDownLoad) base.Render(w);
      } 
      
    0 讨论(0)
  • 2020-11-27 12:26

    For me it helped to register a button that calls code behind code as a postback control.

    protected void Page_Init(object sender, EventArgs e)
    {
        ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(btnMyExport);
    }
    
    0 讨论(0)
  • 2020-11-27 12:28

    This is not issue but this is by design. The root cause is described in Microsoft Support Page.

    The Response.End method ends the page execution and shifts the execution to the Application_EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed.

    The provided Solution is:

    For Response.End, call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_EndRequest event

    Here is the link: https://support.microsoft.com/en-us/help/312629/prb-threadabortexception-occurs-if-you-use-response-end--response-redi

    0 讨论(0)
提交回复
热议问题