【问题标题】:MVC Action taking a long time to returnMVC Action 需要很长时间才能返回
【发布时间】:2009-12-18 14:25:01
【问题描述】:

我有一个带有动作的 MVC 控制器

public ActionResult GeneratePDF(string id)
{
      FileContentResult filePath = this.File(pdfBuffer, MediaTypeNames.Application.Pdf);

      return filePath;
}

由于某种原因,它到达返回线时需要 20 多秒。

pdfBuffer 工作正常,当我在我的 VS 上运行它时一切正常,但是当我部署到 IIS 6 时它运行缓慢。

有人知道为什么吗?

【问题讨论】:

    标签: asp.net-mvc action


    【解决方案1】:

    我在尝试导出到 XLS 和 PDF 时遇到了类似的问题,唯一似乎可以缩短响应时间的方法是直接从生成文件的类发送响应,例如:

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.Buffer = true;
    HttpContext.Current.Response.BufferOutput = true;
    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + file + ".pdf");
    HttpContext.Current.Response.BinaryWrite(stream.ToArray());
    HttpContext.Current.Response.Flush();
    stream.Close();
    HttpContext.Current.Response.End();
    

    但是如果你这样做,你会从 ActionMethod 得到一个"not all code paths return a value",以避免我们只发送一个:

    return new EmptyResult();
    

    最后一行实际上永远不会执行,因为我们直接在方法上结束请求。

    【讨论】:

      猜你喜欢
      • 2014-07-21
      • 1970-01-01
      • 2018-09-10
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 2011-04-03
      相关资源
      最近更新 更多