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

Add controller support to .NET 8 Blazor Wasm

$
0
0

What do I need to do to get controllers added to a .NET 8 Blazor Wasm app? The template now creates the counter and weather, hardcoded in the Razor page which is no use at all.

The template used to add controllers. How do I get this functionality back?

I have tried to set it up by adding builder.Services.AddHttpClient(), UseControllers() etc. but I just get a 404 when trying to call the controller endpoint.

This is my server project program.cs:

using RWeb.Client.Pages;using RWeb.Components;using RWeb.Controllers;var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddRazorComponents()    .AddInteractiveWebAssemblyComponents();builder.Services.AddControllersWithViews(); // Add this linevar app = builder.Build();// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment()){    app.UseWebAssemblyDebugging();}else{    app.UseExceptionHandler("/Error", createScopeForErrors: true);    app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseRouting(); // Add this lineapp.UseAntiforgery();app.UseAuthorization(); // Add this lineapp.MapRazorComponents<App>()    .AddInteractiveWebAssemblyRenderMode()    .AddAdditionalAssemblies(typeof(RecoWeb.Client._Imports).Assembly);app.MapControllers();app.Run();

And this is the program.cs for the client project:

using Microsoft.AspNetCore.Components.WebAssembly.Hosting;var builder = WebAssemblyHostBuilder.CreateDefault(args);builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });builder.Services.AddHttpClient("ServerAPI", client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress));await builder.Build().RunAsync();

Viewing all articles
Browse latest Browse all 4839

Trending Articles



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