代码之家  ›  专栏  ›  技术社区  ›  Coppermill

MVC动作需要很长时间才能返回

  •  1
  • Coppermill  · 技术社区  · 15 年前

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

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

    由于某种原因,当它到达返回线的时候会超过20秒。

    pdfuffer工作正常,当我在vs上运行它时一切正常,但是当我部署到IIS6时,它运行缓慢。

    有人知道为什么吗?

    1 回复  |  直到 15 年前
        1
  •  2
  •   JOBG    15 年前

    在尝试导出到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();
    

    但如果你这样做,你会得到一个 "not all code paths return a value" 为了避免这种情况,我们只发送了一个:

    return new EmptyResult();
    

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