gpt4 book ai didi

asp.net-mvc - 如何从 VOID 方法重定向到 MVC3 中的另一个操作/ Controller ?

转载 作者:行者123 更新时间:2023-12-02 08:15:49 26 4
gpt4 key购买 nike

我有一个返回 void 的 Controller 方法,因为它正在构建一个 Excel 报告供用户下载。我们使用的 Excel 3rd 方库正在写入响应本身。该方法看起来像这样:

[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 中执行此操作......

有什么想法吗?

最佳答案

您可以使用以下代码

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

但是你看过FilePathResult吗?或FileStreamResult

关于asp.net-mvc - 如何从 VOID 方法重定向到 MVC3 中的另一个操作/ Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7352721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com