【发布时间】:2015-05-05 22:41:58
【问题描述】:
好的,
这里我有一个 MVC 4 应用程序,我正在尝试在其中创建一个异步 ActionResult。
目的:用户在网页上有一个下载PDF图标,下载需要很多时间。因此,当服务器忙于生成 PDF 时,用户应该能够在网页中执行一些操作。
(点击“下载PDF”链接正在向服务器发送ajax请求,服务器正在获取一些数据并正在推回PDF)
发生的情况是,当我调用 ajax 下载 PDF 时,它会启动该过程,但会阻止每个请求,除非它返回到浏览器。这是简单的阻塞请求。
到目前为止我已经尝试过什么。
1) 使用 AsyncController 作为控制器的基类。
2) 将 ActionResult 制作为异步任务 DownloadPDF(),在这里我将生成 PDF 的整个代码/逻辑包装到一个包装器中。这个包装器最终在 DownloadPDF() 中是一个可等待的东西
类似的东西。
public async Task<ActionResult> DownloadPDF()
{
string filepath = await CreatePDF();
//create a file stream and return it as ActionResult
}
private async Task<string> CreatePDF()
{
// creates the PDF and returns the path as a string
return filePath;
}
是的,操作是基于会话的。
我是不是在哪里遗漏了什么?
【问题讨论】:
标签: asp.net-mvc-4 asynchronous actionresult async-await asynccontroller