Unable to evaluate expression… on web page

后端 未结 3 1486
既然无缘
既然无缘 2020-12-22 09:24

Related to this question: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

I am currently seeing this in my

3条回答
  •  死守一世寂寞
    2020-12-22 09:39

    The code is fine. When you do Response.End() you get a ThreadAbortException:

    http://support.microsoft.com/kb/312629/EN-US/

    Some people consider Response.End() too drastic though:

    Is Response.End() considered harmful?

    I would suggest handling this specific exception (since you know you are going to get it), and moving the reponse.End() (like mellamokb suggested):

            HttpResponse response = HttpContext.Current.Response;
            try
            {
                DataSet dataSet = new DataSet();
                DataTable table = new DataTable();
    
                dataSet.Tables.Add(table);
    
                response.Clear();
                response.Charset = "";
    
                response.ContentType = "application/vnd.ms-excel";
                response.AddHeader("Content-Disposition", "attachment;filename=\"ExcelFile.xls\"");
    
                using (StringWriter stringWriter = new StringWriter())
                using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
                {
                    DataGrid dataGrid = new DataGrid { DataSource = dataSet.Tables[0] };
    
                    dataGrid.DataBind();
                    dataGrid.RenderControl(htmlTextWriter);
    
                    response.Write(stringWriter.ToString());
    
                }
                response.End();
            }
            catch (ThreadAbortException ex)
            {
                //Log some trace info here
            }
    

提交回复
热议问题