Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4050

Proper way to use HttpClient to avoid null BaseUri

$
0
0

I got this error:

InvalidOperationException: Cannot provide a value for property 'Http' on type 'XBlazor.Components.Themes'. There is no registered service of type 'System.Net.Http.HttpClient'.

Using a simple code with the default template of .NET 9.0:

Themes.razor:

@* @inject HttpClient Http *@@* @inject IJSRuntime JSRuntime *@@namespace  XBlazor.Components<div class="theme-selector"><div class="selected-theme"><span class="dropdown-arrow">▼</span></div></div>

Themes.razor.cs:

using Microsoft.AspNetCore.Components;using Microsoft.JSInterop;namespace XBlazor.Components{    public partial class Themes    {        [Inject]        protected IJSRuntime JSRuntime { get; set; } = null!;        [Inject]        protected HttpClient Http { get; set; } = null!;        [Inject]        protected NavigationManager Nav { get; set; } = null!;        //public Themes(HttpClient http)        //{        //    Http = http;        //}        //protected override void OnInitialized()        //{        //    var baseUri = Nav.BaseUri;        //}        public void Something()        {        }    }}

Program.cs (Server):

using ExDiv.Client.Pages;using ExDiv.Components;var builder = WebApplication.CreateBuilder(args);// Dunno , added recentlybuilder.Services.AddServerSideBlazor()    .AddCircuitOptions(options => options.DetailedErrors = true); // Optional: Enable detail// Add services to the container.builder.Services.AddRazorComponents()    .AddInteractiveServerComponents()    .AddInteractiveWebAssemblyComponents();var app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){    app.UseWebAssemblyDebugging();}else{    app.UseExceptionHandler("/Error", createScopeForErrors: true);    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.    app.UseHsts();}app.UseHttpsRedirection();//app.UseBlazorFrameworkFiles();app.UseAntiforgery();app.MapStaticAssets();app.MapRazorComponents<App>()    .AddInteractiveServerRenderMode()    .AddInteractiveWebAssemblyRenderMode()    .AddAdditionalAssemblies(typeof(ExpandableDiv.Client._Imports).Assembly);// Map the Blazor Server app//app.MapBlazorHub(); // Maps SignalR hub for real-time communication//app.MapFallbackToPage("/_Host"); // Fallback route to load the appapp.Run();

Program.cs (Client):

using Microsoft.AspNetCore.Components;using Microsoft.AspNetCore.Components.Web;using Microsoft.AspNetCore.Components.WebAssembly.Hosting;var builder = WebAssemblyHostBuilder.CreateDefault(args);//builder.RootComponents.Add<App>("#app"); // Adds the app to the <div id="app">//builder.RootComponents.Add<HeadOutlet>("head::after");// Register HttpClient with the base address of the server projectbuilder.Services.AddScoped(sp => new HttpClient{    BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)});//builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });builder.Services.AddScoped(sp =>{    var navManager = sp.GetRequiredService<NavigationManager>();    return new HttpClient { BaseAddress = new Uri(navManager.BaseUri) };});await builder.Build().RunAsync();

Viewing all articles
Browse latest Browse all 4050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>