Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 将datatable导出到电子表格时,在我的C#.NET MVC应用程序中发送HTTP头后,服务器无法设置状态_Asp.net Mvc 3_C# 4.0_Http Headers_Export To Excel - Fatal编程技术网

Asp.net mvc 3 将datatable导出到电子表格时,在我的C#.NET MVC应用程序中发送HTTP头后,服务器无法设置状态

Asp.net mvc 3 将datatable导出到电子表格时,在我的C#.NET MVC应用程序中发送HTTP头后,服务器无法设置状态,asp.net-mvc-3,c#-4.0,http-headers,export-to-excel,Asp.net Mvc 3,C# 4.0,Http Headers,Export To Excel,我正在尝试将datatable导出到电子表格。 我得到了这个例外 发送HTTP头后,服务器无法设置状态。 在这条线上 httpContext.Response.StatusCode=ex是HttpException吗?((HttpException)ex).GetHttpCode():500 请帮助我如何解决这个问题 为了便于参考,我在这里添加了完整的代码: 导出功能的代码为 public static void ExportToSpreadsheet(DataTable table, s

我正在尝试将datatable导出到电子表格。 我得到了这个例外

发送HTTP头后,服务器无法设置状态。

在这条线上

httpContext.Response.StatusCode=ex是HttpException吗?((HttpException)ex).GetHttpCode():500

请帮助我如何解决这个问题

为了便于参考,我在这里添加了完整的代码: 导出功能的代码为

   public static void ExportToSpreadsheet(DataTable table, string name)
            {
                HttpContext context = HttpContext.Current;
                context.Response.Clear();

                foreach (DataColumn column in table.Columns)
                {
                    context.Response.Write(column.ColumnName + ";");
                }

                context.Response.Write(Environment.NewLine);

                foreach (DataRow row in table.Rows)
                {
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");
                    }
                    context.Response.Write(Environment.NewLine);
                }

                string saveAsFileName = string.Format("Results-{0:d}.xls", DateTime.Now);

                context.Response.ContentType = "application/vnd.ms-excel";
                context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
                context.Response.End();
            }
protected void Application_Error(object sender, EventArgs e)
        {
            var httpContext = ((MvcApplication)sender).Context;
            //var currentController = " ";
            //var currentAction = " ";
            string currentController;
            string currentAction;
            var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
            currentController = "";
            currentAction = "";
            if (currentRouteData != null)
            {
                if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
                {
                    currentController = currentRouteData.Values["controller"].ToString();
                }

                if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
                {
                    currentAction = currentRouteData.Values["action"].ToString();
                }
            }
            else
            {
                currentController = "Home";
                currentAction = "Index";
            }

            var ex = Server.GetLastError();
            var controller = new ErrorController();
            var routeData = new RouteData();
            var action = "Error";

            if (ex is HttpException)
            {
                var httpEx = ex as HttpException;

                switch (httpEx.GetHttpCode())
                {
                    case 400:
                        action = "BadRequest";
                        break;

                    case 401:
                        action = "Unauthorized";
                        break;

                    case 403:
                        action = "Forbidden";
                        break;

                    case 404:
                        action = "NotFound";
                        break;

                    case 408:
                        action = "RequestTimeout";
                        break;

                    case 500:
                        action = "InternalServerError";
                        break;

                    case 502:
                        action = "BadGateway";
                        break;

                    case 503:
                        action = "ServiceUnavailable";
                        break;

                    case 504:
                        action = "GatewayTimeout";
                        break;
                }
            }

            httpContext.ClearError();
            httpContext.Response.Clear();
            // on this below line exception occurs
            httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
            httpContext.Response.TrySkipIisCustomErrors = true;

            routeData.Values["controller"] = "Error";
            routeData.Values["action"] = action;

            controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction); 
            ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));

            // to set error details in HandleErrorInfo model to send email notification
            ErrorController.setHandleErrorInfoModel(new HandleErrorInfo(ex, currentController, currentAction));                
        }