首页 > 解决方案 > 响应刷新后重定向到路由

问题描述

我正在使用 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 之前,它会重定向,但文件不会下载到浏览器。FlushEndRedirectToRoute

标签: c#httpresponsehttp-redirectspreadsheetgear

解决方案


推荐阅读