Reputation: 1
WebAssembly Program.cs configuration File:
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection.Extensions;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.TryAddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost:7224/") });
await builder.Build().RunAsync();
Using httpclient in this OnInitializedAsync will result in an error if no exception catch is added:
System.InvalidOperationException:An invalid request URI was provided. Either the request URI must be an absolute URI or BaseAddress must be set.
var res = await _httpClient.GetFromJsonAsync<List<Product>>("api/User");
Exception catching is OK again Thank you.
Upvotes: 0
Views: 129
Reputation: 1693
Its sounds like the httpclient is being passed in with a null URI. But you are initializing the URI on the client side registration. I had a similar issue where the razor component was being loaded twice, once on the server, then on the client.
In the razor component on the client that performs the following.
var res = await _httpClient.GetFromJsonAsync<List<Product>>("api/User");
Add the following to the top of that razor component.
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false))
This will disable the pre-rendering and your page should load only once using the registered services from the client side.
Upvotes: 0