Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Redirect to another Action/Controller in MVC3 from a VOID method?

I have a controller method that returns a void because it is building an Excel report for the user to download. The Excel 3rd party library we're using is writing to the response itself. The method looks something like this:

[HttpGet]
public void GetExcel(int id)
{
  try
  {
    var report = _reportService.GetReport(id);
    var table = _reportService.GetReportTable(id);
    var excelReport = new ExcelReport(table, report.Name);
    excelReport.DownloadReport(System.Web.HttpContext.Current.Response);
  }
  catch (Exception ex)
  {
    // This is wrong, of course, because I'm not returning an ActionResult
    Response.RedirectToRoute("/Report/Error/", new { exceptionType = ex.GetType().Name });
  }
}

There are several security checks in place that throw exceptions if the user doesn't meet certain credentials for fetching the report. I want to redirect to a different page and pass along some information about the exception, but I can't figure out how to do this in MVC3....

Any ideas?

like image 423
Chris Holmes Avatar asked Sep 08 '11 18:09

Chris Holmes


2 Answers

You could use the following code

Response.Redirect(Url.Action("Error", "Report", new { exceptionType = ex.GetType().Name }));

But have you taken a look at the FilePathResult or FileStreamResult ?

like image 52
Major Byte Avatar answered Nov 05 '22 21:11

Major Byte


Instead of letting the 3rd part library write to the response directly get the content use regular ActionResult and return File(...) for the actual file or RedirectToAction(...) (or RedirectToRoute(...)) on error. If your 3rd party library can only write to Response you may need to use some tricks to capture it's output.

[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  });
  }
}
like image 2
Cymen Avatar answered Nov 05 '22 19:11

Cymen