错误无法评估expression式,因为代码已经优化

我想出口Excel与StreamWriter。 但错误:无法评估expression式,因为代码已经优化,或者本地框架位于调用堆栈之上。

代码C#:

private void ExportToExcel() { string filePath = string.Empty; try { if (fn.CheckRowOnDataTable(dtExport)) { string path = Server.MapPath(@"~/EXCEL/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string fileName = string.Format("{0}_{1}.xls", this.PROGRAM_ID, DateTime.Now.ToString("yyyyMMdd_HHmmss")); filePath = string.Format("{0}{1}", path, fileName); if (File.Exists(filePath)) { File.Delete(filePath); } using (StreamWriter writer = new StreamWriter(filePath)) { writer.Write(GenerateExcel()); } HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("filename={0}", fileName)); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.WriteFile(filePath); <<< Line Error HttpContext.Current.Response.End(); } else { ..... } } catch (OverflowException e) { ..... } catch (Exception ex) { ..... } } 

如何解决这个问题? 感谢提前;)

试试这个:代替HttpContext.Current.Response.End()使用HttpContext.Current.ApplicationInstance.CompleteRequest()