这可能很简单,但这里是:
我正在我的 MVC3 应用程序中实现一个 excel 可下载报告。我过去使用过这种方法并且效果很好,但是在这种情况下,报表中可能不存在销售数据。这是我的代码:
我在 Reports Controller 中有一个 FileResult 操作:
[HttpPost]
public FileResult ExcelReportDownload(ReportExcelDownloadRequest reportRequest)
{
ReportEngine re = new ReportEngine();
Stream report = re.GetReport(reportRequest);
return new FileStreamResult(report, "application/ms-excel")
{
FileDownloadName = "SalesReport.xls"
};
}
我的问题是,有时报告流可能为空,这意味着没有可用的销售信息,在这种情况下,我宁愿重定向到显示消息的 View ,表明没有可用的销售信息,但我不确定如何实现这一点。
有办法吗?
最佳答案
嗯,FileResult
继承自 ActionResult
:
如果您的结果可以是 RedirectToRouteResult
(继承自 ActionResult
)或 FileResult
,那么……您的操作必须是输入 ActionResult
,它可以管理两者。
类似的东西:
[HttpPost]
public ActionResult ExcelReportDownload(ReportExcelDownloadRequest reportRequest)
{
ReportEngine re = new ReportEngine();
Stream report = re.GetReport(reportRequest);
if (report == null)
return RedirectToAction(<action Name>);
else
return new FileStreamResult(report, "application/ms-excel")
{
FileDownloadName = "SalesReport.xls"
};
}
关于c# - 从 FileResult 重定向到 MVC ActionResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11434345/