Instead of letting the 3rd part library write the answer directly, use the usual ActionResult to get the contents and return File(...) for the actual file or RedirectToAction(...) (or RedirectToRoute(...) ) if an error occurs. If your third-party library can only write in Response, you may need to use some tricks to output it.
[HttpGet] public ActionResult GetExcel(int id) { try { var report = _reportService.GetReport(id); var table = _reportService.GetReportTable(id); var excelReport = new ExcelReport(table, report.Name); var content = excelReport.MakeReport(System.Web.HttpContext.Current.Response); return File(content, "application/xls", "something.xls"); } catch (Exception ex) { RedirectToRoute("/Report/Error/", new { exceptionType = ex.GetType().Name }); } }
Cymen
source share