A view component defines InvokeAsync when its work uses asynchronous APIs. The method returns a Task; Razor waits for it to complete before inserting the result into the parent view or Razor Page. A synchronous view component defines Invoke instead. section 24.5.2
Fetch a page size
In Components/PageSize.cs, a ViewComponent defines `public async Task<IViewComponentResult> InvokeAsync()`. It awaits `HttpClient.GetAsync("http://microsoft.com")`, then returns `View(response.Content.Headers.ContentLength)`. The default view at Views/Shared/Components/PageSize/Default.cshtml displays that length. Add `<vc:page-size />` to a controller view to include the result; the calling syntax does not change because the component is asynchronous. section 24.5.2
Listing 24.8 Using a view component in the Index.cshtml file in the Views/Home folder
@model Product?
@{
Layout = "_Layout";
ViewBag.Title = "Product Table";
}
@section Header {
Product Information
}
<tr><th>Name</th><td>@Model?.Name</td></tr>
<tr>
<th>Price</th>
<td>@Model?.Price.ToString("c")</td>
</tr>
<tr><th>Category ID</th><td>@Model?.CategoryId</td></tr>
@section Footer {
@(((Model?.Price / ViewBag.AveragePrice)
* 100).ToString("F2"))% of average price
}
@section Summary {
<div class="bg-info text-white m-2 p-2">
@await Component.InvokeAsync("CitySummary")
</div>
}
The controller-view listing shows the alternative helper call, `@await Component.InvokeAsync("CitySummary")`. section 24.3.1
Do not confuse the component's InvokeAsync method with `Component.InvokeAsync` in a parent view. The parent always uses that helper method—even when the component itself defines synchronous Invoke. Its first argument names the component; an optional second object supplies values matching the component method's parameters. Missing required values can leave the component absent without an error message. section 24.3.1 section 24.5.1
The name also appears in Blazor, but there it denotes different APIs: a Razor component uses `InvokeAsync` to run a function inside the Blazor environment, while `EventCallback<T>.InvokeAsync` triggers a custom event. Neither is the view-component method that produces content for a parent view. section 35.4.2 section 34.2.2
