gpt4 book ai didi

c# - 如果成功则返回 FileStreamResult,如果不成功则返回 View

转载 作者:行者123 更新时间:2023-11-30 15:21:17 26 4
gpt4 key购买 nike

在 MVC 中,有没有办法在我的查询成功时返回一个 FileStreamResult,但如果不成功,就在 ViewBag 中返回一个有错误的 View 。像这样....

    public FileStreamResult Submit()
{
string retVal;
try
{
DataSet ds = new DataSet();

ds = getDS();
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
ExcelXmlWorkbook book = ExcelXmlWorkbook.DataSetToWorkbook(ds);
string fileName = "results.xml";
book.Export(fileName, ExportFormat.Xml, 0);
FileInfo info = new FileInfo(fileName);
return File(info.OpenRead(), "application/x-msexcel");
}
else
{
retVal = "No Data";
}

}
catch (Exception ex)
{
retVal = ex.Message;
}

ViewBag.Result = retVal;

return View("ViewName");
}

我知道这行不通,但基本上,如果数据拉取成功,我想打开一个结果文件...如果不成功,我想显示该页面,或重定向到另一个页面以显示结果失败的用户。也欢迎任何关于更好方法的建议。谢谢!

最佳答案

ViewResult(View 方法返回的类型)和 FileStreamResult 都派生自 ActionResult类,表示操作方法的结果,因此您必须从 Submit 返回它的实例。

public ActionResult Submit()

关于c# - 如果成功则返回 FileStreamResult,如果不成功则返回 View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37930714/

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