Seph
Seph

Reputation: 8703

http calls in blazor interactiveauto rendermode refreshes run twice

When using RenderMode.InteractiveAuto in the new .NET8 RC2. When trying to make HTTP calls the page reloads after under 1second and I can't figure out why.

The example code I'm using was from https://github.com/SpaceShot/InteractiveAutoSample/tree/main

Uncommenting the last line in Home.razor

<WebScrape></WebScrape>

And changing the last line in WebScrape.razor to have the full URL rather than a relative path:

var response = await Http.GetFromJsonAsync<string>("https://localhost:7202/webscraper/bing"); // change port here to the hosted port

Page loads, displays the html string from the Http call and then displays Loading... and then displays the Html string again.

Upvotes: 2

Views: 687

Answers (2)

gwruck
gwruck

Reputation: 793

InteractiveAuto will first try to load using InteractiveServer (SSR) (ie. the first load) and then will follow with WASM (Client Side Rendering CSR) once the Blazor Bundle is downloaded (ie. the second load).

Try this link for the official explanation ASP.NET Core Blazor render modes

Upvotes: 0

Brian Parker
Brian Parker

Reputation: 14573

Add data-permanent to the containing div. To preserve the state between renders.

<div style="border : solid; border-color : black; padding: 4px;" data-permanent>
    @Text
</div>

The other issue is the component renders twice. Once on the server for prerendering the default (controlled by the containing pages' rendermode) then and once for the RenderMode active (initial view ServerSide subsequent views WASM) so the injection for the http client has to be set up on both.

All 3 interactive modes have a parameter prerender that is defaulted to true.

You can do this:

@attribute [RenderModeInteractiveAuto(prerender: false)]

This answer may help with the server base address: https://stackoverflow.com/a/63833663/1492496

Upvotes: 2

Related Questions