我想在MVC 6中實現一個異步組件,我掙扎着。異步組件不工作在MVC 6
查看代碼:
@await Component.InvokeAsync("GetTextA")
@Component.Invoke("GetTextB")
構成元素A碼:
public class GetTextAViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync()
{
await Task.Delay(12000);
return View("Default","from code");
}
}
組分B編碼:
public class GetTextBViewComponent : ViewComponent
{
public IViewComponentResult Invoke()
{
return View("Default");
}
}
它需要多於12000毫秒加載視圖。這意味着異步組件被同步加載。
如何異步加載視圖中的所有內容,而不是等待異步組件加載。
嗯,你告訴任務等待12000毫秒... HTTPS://msdn.microsoft.com/en-us/ library/hh194873%28v = vs.110%29.aspx –
是的,等待代碼有意添加,以找到異步任務同步工作的原因 – Rad
您的異步任務不工作同步,它的事實,視圖必須等待它在渲染之前完成。 –