我有一个试图检索 IEnumerable<Post>
的部分 View 使用异步从数据库...
方法
public static class PostService
{
public static int PostsPerPage = 50;
public static async Task<IEnumerable<Post>> GetRecentAsync(int page = 0)
{
return await entityFrameworkDbContext.Posts
.ToListAsync();
}
}
局部 View
public async Task<ActionResult> Recent(int page = 0)
{
return PartialView(await PostService.GetRecentAsync(page));
}
然后如果我尝试调用它
@Html.Action("Recent", "Post")
我得到以下异常
HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.
为什么会出现此错误?它不应该起作用吗?
最佳答案
子 Action 必须同步调用。 Issue 601我也不知道当前 MVC 库的任何最新更新允许此功能。
Issue 601 的评论暗示此功能已添加到 MVC vNext 中,又名。 MVC6。子操作看起来被替换为 ViewComponent
,它可以从如下 View 异步调用。完整示例 here和 here
<div>
@await Component.InvokeAsync("YourComponent")
</div>
有关 MVC6 的更多信息,请查看 http://www.asp.net/vnext/overview/aspnet-vnext/overview
注意:这个回答只是走个形式,所以问题可以标记为已回答
关于c# - Async PartialView 导致 "HttpServerUtility.Execute blocked..."异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24072720/