MVC Action需要很长时间才能返回 [英] MVC Action taking a long time to return

查看:113
本文介绍了MVC Action需要很长时间才能返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an MVC Controller with an action

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

      return filePath;
}

由于某种原因,到达返回线要花费20秒钟以上

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

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

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.

有人知道为什么吗?

推荐答案

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

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.

这篇关于MVC Action需要很长时间才能返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆