ASP.NET MVC 如何从控制器操作中输出 FileContent 并继续

发布于 2024-10-17 13:38:43 字数 545 浏览 7 评论 0原文

我对 MVC 相当陌生,想知道正确的方法是什么 - 提交表单时执行编辑 ActionResult,保存数据后,我想生成一个文件并发送到浏览器进行下载,但也继续另一个动作。

文件被发送到浏览器,但控制器中没有进行进一步处理,因为我使用了 return RedirectToActions。预先感谢您的任何指点。

public ActionResult Edit(int id, InvoiceFormData formData)  
{ 
  ...    
  return base.RedirectToAction("ReturnPdf", new { id = 99 });

  // More processing...ideally...then

  return base.RedirectToAction("Action", "Controller");
}

public FileResult ReturnPdf(int? id)
{
  ...
  return output; // writes file output and prompts user to download
}

I am fairly new to MVC and want to know what the correct approach is - when a form is submitted the Edit ActionResult is executed, once the data is saved, I want to generate a file and send to the browser for download but also continue to another action.

The file is sent to the browser but no further processing occurs in the controller, because I use return RedirectToActions. Thanks in advance for any pointers.

public ActionResult Edit(int id, InvoiceFormData formData)  
{ 
  ...    
  return base.RedirectToAction("ReturnPdf", new { id = 99 });

  // More processing...ideally...then

  return base.RedirectToAction("Action", "Controller");
}

public FileResult ReturnPdf(int? id)
{
  ...
  return output; // writes file output and prompts user to download
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街九尾狐 2024-10-24 13:38:43

您不能在同一操作结果中使用两个 return 语句并期望它们都运行。当您“返回”时,意味着您正在返回输出,然后退出操作结果。因此,您可以链接您的操作,这样您可以将该逻辑移至 ReturnPdf AR 中(因为这就是我在代码中看到的内容),而不是在编辑操作结果中进行进一步处理,然后最终返回最终输出无论您达到什么行动结果。我希望这已经足够清楚了......

You cannot use two return statements in the same action result and expect them to both run. When you "return" it means you're returning the output and then quiting out of the action result. Therefore, you could kind of chain your actions, so rather than doing the further processing inside the Edit action result, you could move that logic into the ReturnPdf AR(since that's what's I could see in your code), then finally return the final output in whatever action result you landed on. I hope that was clear enough...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文