2

Why does this code work perfectly, outputting the HTML as expected and...

<div class="page home-page custom-container d-flex flex-column">
    @await Component.InvokeAsync("PersonalizedProducts")
    @await Component.InvokeAsync("RecommendedProducts")
    @await Component.InvokeAsync("SuggestedProducts")
    @await Component.InvokeAsync("HomePageProducts")
    @await Component.InvokeAsync("HomePageNewProducts")
    @await Component.InvokeAsync("CategoryFeaturedProducts")
</div>

...this one doesn't work:

<div class="page home-page custom-container d-flex flex-column">
@{
    var tasks = new List<Task>();

    tasks.Add(Component.InvokeAsync("PersonalizedProducts"));
    tasks.Add(Component.InvokeAsync("RecommendedProducts"));
    tasks.Add(Component.InvokeAsync("SuggestedProducts"));
    tasks.Add(Component.InvokeAsync("HomePageProducts"));
    tasks.Add(Component.InvokeAsync("HomePageNewProducts"));
    tasks.Add(Component.InvokeAsync("CategoryFeaturedProducts"));

    await Task.WhenAll(tasks);
}
</div>

The components mentioned above are 100% independent between themselves and do not share anything but a database, which they do no alter since these are fetch-only components...

2
  • whats the error you are getting? can you post that Commented Dec 4, 2019 at 18:45
  • There is no actual error, the page just comes back blank... Commented Dec 4, 2019 at 19:20

1 Answer 1

1

If you are expecting html output then you will need to add more code to output the html:

<div class="page home-page custom-container d-flex flex-column">
@{
    var tasks = new List<Task>();

    tasks.Add(Component.InvokeAsync("PersonalizedProducts"));
    tasks.Add(Component.InvokeAsync("RecommendedProducts"));
    tasks.Add(Component.InvokeAsync("SuggestedProducts"));
    tasks.Add(Component.InvokeAsync("HomePageProducts"));
    tasks.Add(Component.InvokeAsync("HomePageNewProducts"));
    tasks.Add(Component.InvokeAsync("CategoryFeaturedProducts"));

    await Task.WhenAll(tasks);

    foreach( var t in tasks)
    {
        var var1 = await t;
        @var1;
    }
}
</div> 
Sign up to request clarification or add additional context in comments.

1 Comment

Almost! the secret was to declare List<Task<Microsoft.AspNetCore.Html.IHtmlContent>>!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.