[英]Redirecting to MVC ActionResult from FileResult
This might be a simple one but here goes: 这可能是一个简单的,但这里是:
I'm implementing an excel downloadable report in my MVC3 application. 我正在我的MVC3应用程序中实现一个excel可下载的报告。 I've used this method in the past and it's worked perfectly, however in this case, there is a chance that sales data may not exist for the report.
我过去曾使用过这种方法而且效果很好,但在这种情况下,报告中可能不存在销售数据。 Here is my code:
这是我的代码:
I have a FileResult action within a Reports controller: 我在Reports控制器中有一个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"
};
}
My issue is that sometimes the report stream may be null meaning that there's no sales info available, in which case I would rather redirect to a View that displays a message to say there is no sales information available, however I am not sure how to achieve this. 我的问题是,有时报告流可能为空意味着没有可用的销售信息,在这种情况下我宁愿重定向到显示消息的视图,说没有可用的销售信息,但是我不知道如何实现这个。
Is there a way to do this? 有没有办法做到这一点?
Well, FileResult
inherits from ActionResult
: 好吧,
FileResult
继承自ActionResult
:
If you result can be either a RedirectToRouteResult
(inheriting from ActionResult
) or a FileResult
, then... your action must be of type ActionResult
, which can manage both. 如果结果可以是
RedirectToRouteResult
(继承自ActionResult
)或FileResult
,那么......您的操作必须是ActionResult
类型,它可以管理两者。
something like that : 类似的东西:
[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"
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.