How to host server side controllers, with a Blazor Web App (WebAssembly), in .NET 8?
Asked Answered
R

3

7

Before .NET 8, hosting server side controllers with a Blazor WebAssembly app was easy. You just clicked the "ASP.NET Core Hosted" checkbox in the project template, and an ASP.NET Core project was created for you, that would host the Blazor WebAssembly client.

With the release of .NET 8, a new template has been introduced called "Blazor Web App". But it does not have the "ASP.NET Core Hosted" checkbox

enter image description here

How am i suposed to host server side controllers then? Does "Blazor Web App" introduce a new system for handling client to server communication?

Microsoft documentation says:

We removed the Blazor Server template, and the ASP.NET Core Hosted option has been removed from the Blazor WebAssembly template. Both of these scenarios are represented by options when using the Blazor Web App template.

But i am not sure how this scenario is "represented by options when using the Blazor Web App template"

Roa answered 15/11, 2023 at 9:26 Comment(0)
G
15

When you create a "Blazor Web App" with an Interactivity that includes Wasm you already get a Client and a Server project. The Shared (DTO) project is missing but you can easily add it, it's just a class library.

Add AddControllers() and MapControllers() to the server's Program.cs and then you can start adding the controllers you need.

   ...

builder.Services.AddControllers();
var app = builder.Build();

   ...

app.MapControllers();
app.Run();
Gratia answered 15/11, 2023 at 10:19 Comment(4)
It works, but I found a problem with Authentication/Authorization : if my page on the Client is protected with @attribute [Authorize] and my Controller on the server is protected by [Authorize], I can login and access the page, but the Controller does not get the Authorization. Do you have any idea why ?Amarillis
Perhaps this could help. I've seen that in InteractiveWebAssembly rendermode there are two calls to the page (I load the fields from the controller in OnParametersSetAsync). In the first call authorization fails but on the second call it works. My page was failing since I had a throw exception when the request to the controller failed. If I take it out and substitute it to return an empty list then authorization works on the second time and the page loads without problems.Spoondrift
I add to my previous comment that InteractiveServer rendermode is not authorizing with the controller. I guess that in this case, an Interface must be used and implement access to the service directly for the server and use of the controller for the WebAssemblySpoondrift
I'm running into this same situation, trying to enforce auth on my controllers but it doesn't seem compatible with blazor. Did you ever find a solution?Perlite
I
2

This video provides a good explanation of how to achieve the traditional .NET 7 hosted WASM solution structure using .NET standalone Blazor and web API projects. https://youtu.be/3Ur79_kHVpo?si=BRzqU4ZCdibtzKWc

Impanation answered 7/6 at 17:44 Comment(0)
L
1

What had worked for me was selecting "Blazor WebAssembly Standalone App" then change Framework to .Net 7. The "ASP.Net Core Hosted" option will be available. Proceed with that checked and then upgrade to .Net 8 after the project has been created. That's the laziest way of getting around that but seems to be working for me.

Lontson answered 27/12, 2023 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.