如何从VOID方法重定向到MVC3中的另一个动作/控制器? [英] How do I Redirect to another Action/Controller in MVC3 from a VOID method?

查看:130
本文介绍了如何从VOID方法重定向到MVC3中的另一个动作/控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回空值的控制器方法,因为它正在构建供用户下载的Excel报表.我们正在使用的Excel 3rd party库正在编写响应本身.该方法如下所示:

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 });
  }
}

如果用户不符合特定的凭证来获取报告,则会进行多项安全检查,从而引发异常.我想重定向到另一个页面并传递有关该异常的一些信息,但是我不知道如何在MVC3中执行此操作....

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....

有什么想法吗?

推荐答案

您可以做

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

但是您是否看过 FilePathResult FileStreamResult 吗?

这篇关于如何从VOID方法重定向到MVC3中的另一个动作/控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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