繁体   English   中英

System.Threading.ThreadAbortException中未更新标签

[英]label not updated in System.Threading.ThreadAbortException

在我的页面中,我正在尝试下载文件。 该文件已成功下载,但出现System.Threading.ThreadAbortException。 所以我在尝试捕获块中处理了该问题,并将错误标签设置为空白,但页面中没有更新。

        catch (System.Threading.ThreadAbortException)
        {
            lblErrorMsg.Text = "dfdf";
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = "Error while processing you request :<br> Erorr Description : " + ex.Message;
        }

这是我的写入文件功能

  public static void WriteFile(string nameOfFile, string fileContent, HttpResponse writer)
    {
        writer.ClearHeaders();
        writer.ClearContent();

        writer.ContentType = "text/xml";
        writer.AppendHeader("Content-Disposition", "attachment; filename=" + nameOfFile);
        writer.Write(fileContent);
        writer.Flush();
        writer.End();
    }

有人可以告诉我为什么在调试代码时,即使标签位于system.thread.threadabortexceptiopn的Catch块下,标签也不设置为空白?

发生ThreadAbortException是因为您通过调用Response对象的End()方法过早关闭了Response。 这也解释了为什么现在写页面内容为时已晚。 这不是一个很烦人的错误,但是最好进行整洁的处理。

只需检查以下答案, 为什么Response.Redirect会导致System.Threading.ThreadAbortException? 如何避免Response.End()“线程被中止”在Excel文件下载期间发生异常,以及与Response和ThreadAbortException相关的其他答案,以了解它并通过编写更好的文件下载代码来正确处理(根据您的用法)。

另外请注意,对于页面和页面上的某些内容(例如标签)完全重写的响应流没有什么意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM