I feel this question has not an accurate response so I will try to detail.
First to know is about what @Robula commented.
Methods involved in component instance creation (OnInitialized, OnParametersSet) are called twice always that we are using "ServerPrerendered". More info
I don't think @PeterMorris answer is accurate since I dont feel JS or Parameter dependencies are involved in this decision of calling an API or long-running ops in one or another method.
Being coherent the most optimal implementation should be call those in the OnAfterRender/OnAfterRenderAsync method and only under the flag "firstRender == true".
As @MofaggolHoshen said.
<div class="container">
<div class="row">
@Text
</div>
</div>
@code{
private string Text { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
HttpClient client = new HttpClient();
Text = await client.GetStringAsync("https://filesamples.com/samples/document/csv/sample2.csv");
StateHasChanged();
}
await base.OnAfterRenderAsync(firstRender);
}
}
This way we ensure Api calls or long-running ops are called only one time and rendered asynchronously.