从FileResult重定向到MVC的ActionResult [英] Redirecting to MVC ActionResult from FileResult

查看:254
本文介绍了从FileResult重定向到MVC的ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的人,但这里有云:

This might be a simple one but here goes:

我实施我MVC3应用程序的Excel下载的报告。我用在过去此方法和它的工作完全,但是在这种情况下,有机会的销售数据可能没有针对报告存在。这里是我的code:

I'm implementing an excel downloadable report in my MVC3 application. 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:

我有一个报告控制器内的FileResult动作:

I have a FileResult action within a Reports controller:

    [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?

推荐答案

好了, FileResult 的ActionResult 继承:

如果您导致可以是 RedirectToRouteResult (继承的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.

这样的事情:

    [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"
           };
    }

这篇关于从FileResult重定向到MVC的ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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