无法评估表达式,因为代码已优化或本机框架c# [英] unable to evaluate expression because the code is optimized or a native frame c#

查看:150
本文介绍了无法评估表达式,因为代码已优化或本机框架c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void ExportToExcel(DataTable dt, string FileName)
      {
         
              System.Web.HttpContext.Current.Response.Clear();
              System.Web.HttpContext.Current.Response.Buffer = true;
              System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + DateTime.Now + ".xls");
              System.Web.HttpContext.Current.Response.Charset = "";
              System.Web.HttpContext.Current.Response.ContentType = "application/text";
              StringBuilder sb = new StringBuilder();
              sb.Append("<table cellSpacing='0' cellPadding='0' align='center' border='1'><tr>");

              for (int k = 0; k <= dt.Columns.Count - 1; k++)

              {

                  //add separator

                  //& sw.ToString() & "</table>")
                  sb.Append("<th align ='Left' width = '50px'>");
                  sb.Append(dt.Columns[k].ColumnName.ToString());
                  sb.Append("</th>");
              }
              sb.Append("</tr>");
              //append new line
              //sb.Append(Constants.vbCr + Constants.vbLf);
              for (int i = 0; i <= dt.Rows.Count - 1; i++)

              {

                  sb.Append("<tr>");
                  for (int k = 0; k <= dt.Columns.Count - 1; k++)

                  {

                      //add separator

                      string retval = dt.Rows[i][k].ToString();

                      sb.Append("<td align ='Left' width = '50px'>");
                      sb.Append(dt.Rows[i][k].ToString().Replace(",", ";"));
                      sb.Append("</td>");
                  }
                  sb.Append("</tr>");
                  //append new line
                  //sb.Append(Constants.vbCr + Constants.vbLf);
              }
              sb.Append("</table>");
              System.Web.HttpContext.Current.Response.Output.Write(sb.ToString());
              System.Web.HttpContext.Current.Response.Flush();
              //HttpContext.Current.ApplicationInstance.CompleteRequest();

              System.Web.HttpContext.Current.Response.End();
          }
         
          }
}

推荐答案

确保没有其他在 Response.End()之后执行的逻辑。

Response.End()应该抛出这个设计的异常。 br />
您可以尝试通过调用 ApplicationInstance.CompleteRequest()而不是 Response.End()来解决此问题。

示例:

Make sure that there is no other logic to execute after the Response.End().
Response.End() is supposed to throw this exception which is by design.
You can try to resolve this by calling ApplicationInstance.CompleteRequest() instead of Response.End() .
Example:
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();





参考:

如果使用响应,则会发生ThreadAbortException。 End,Response.Redirect或Server.Transfer [ ^ ]



希望,它有帮助:)



Reference:
ThreadAbortException Occurs If You Use Response.End, Response.Redirect, or Server.Transfer[^]

Hope, it helps :)


这篇关于无法评估表达式,因为代码已优化或本机框架c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆