MVC Action 需要很长时间才能返回

发布于 2024-08-15 22:21:32 字数 326 浏览 1 评论 0原文

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

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

      return filePath;
}

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

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

有人知道为什么吗?

I have an MVC Controller with an action

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

      return filePath;
}

And for some reason it is taking over 20 seconds when it hits the return line.

The pdfBuffer is working okay, and when I run it on my VS all is okay, but when I deploy to IIS 6 it runs slow.

Anyone know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2024-08-22 22:21:32

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

但如果这样做,您将得到一个 < code>“并非所有代码路径都从 ActionMethod 返回值”,以避免我们只发送:

return new EmptyResult();

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

I was running into a similar issue when trying to export to XLS and PDF, the only thing that seem to improve the response time was sending the response directly from the class that generates the file like:

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();

But if you do this you will get a "not all code paths return a value" from the ActionMethod, to avoid that we just send a:

return new EmptyResult();

This last line will actually never be executed because we end the request directly on the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文