无法评估表达式...在网页上 [英] Unable to evaluate expression... on web page

查看:118
本文介绍了无法评估表达式...在网页上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此问题相关:无法评估表达式,因为代码已优化,或本机框架位于调用堆栈顶部

我目前在我的例外:


{无法评估表达式,因为代码已优化,或本机框架位于调用堆栈之上} >

{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

这是违规代码。异常被抛出在response.End();

Here's the offending code. The exception is thrown on response.End();

DataSet dataSet = new DataSet();
dataSet.Tables.Add(table); 
// Table is a well-formatted DataTable formed from data stored in a Session variable

HttpResponse response = HttpContext.Current.Response;
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();
}

此代码正在网络上的导出到Excel按钮中使用页。这是直接从使用与正常工作相同的功能的另一个页面复制的。

This code is being used in an "export to excel" button on a web page. This is copied directly from another page that uses the same functionality that works correctly.

有关如何调试此问题的任何想法?我怎么能看到这个例外呢?此外,相关问题在这里如何适用?

Any ideas on how to debug this issue? How can I get to a point where I can see the exception? Also, how does the related question apply here? The top answer and selected answers are incredibly vague.

请注意,中的数据存储在会话状态

Please note that the data in table is stored in Session state.

提前感谢

推荐答案

代码很好。当你做Response.End()时,你会得到一个ThreadAbortException:

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

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

有些人认为回应.End()太激烈了:

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

Response.End()被认为是有害的?

我建议处理这个特定的异常(因为你知道你会得到它),并移动reponse.End()(如mellamokb建议):

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
        }

这篇关于无法评估表达式...在网页上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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