c# - 响应刷新后重定向到路由
问题描述
我正在使用 Spreadsheet Gear 生成 Excel 文档并将其下载到客户端浏览器。使用下面的代码可以正确下载 excel 文档,但我需要浏览器然后将用户重定向到当前页面。我哪里错了?
控制器
[HttpPost]
public ActionResult Checkout(Models.Checkout m, int id)
{
var model = this.CreateModel<Models.Checkout>();
if (model.Checkout().Success)
{
this.HandleExport(result.Workbook, "Inspection", new { controller = "Checkout", id = id, action = "Detail" });
}
return View(model);
}
处理导出
public void HandleExport(IWorkbook workbook, string fileName, object routeValues)
{
HttpContext.Response.Clear();
HttpContext.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Response.AddHeader("Content-Disposition", $"attachment; filename =\"{fileName}_{ DateTime.Now:yyyy-MM-dd_hh-mm-ss}\".xlsx");
workbook.SaveToStream(HttpContext.Response.OutputStream, FileFormat.OpenXMLWorkbook);
HttpContext.Response.Flush();
HttpContext.Response.End();
HttpContext.Response.RedirectToRoute(routeValues);
}
我试过在之后但之前设置RedirectToRoute
呼叫,但它仍然没有重定向。如果我在 Flush 之前,它会重定向,但文件不会下载到浏览器。Flush
End
RedirectToRoute
解决方案
推荐阅读
- kubernetes - 为 Raspberry Pi 集群创建 Folding@Home 的 k8s 部署的好处?
- javascript - JS/CSS:除非在 setTimeout 内,否则不会发生转换
- python - Google Colab RAM 不断被用完
- javascript - Javascript 中未定义 KeyCode
- android - Android - 使用 Compose 创建 Spinner
- javascript - 更新对象嵌套字段中的值 - Firestore
- azure - 有没有办法备份我所有的天蓝色服务数据
- google-apps-script - 谷歌表格脚本:搜索任何有错误的单元格
- angular - 无法在 Angular 上获取 API 数据
- c - 使用 getchar 从文件中读取字符串