Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Blazor Assembly with ASP.net Core Hosted Model

I've been reading quite a few articles about Blazor and the different hosting models (3 actually). The easiest to understand is the pure client model, which is a standalone app that runs on webassembly. No problem there. But I continue to struggle to understand the difference between the server model and the WebAssembly model with ASP.NET Core Hosted (a checkbox) model.

Please help me clarify a few things.

1) For the server model, the browser connects to the server app using signalR. It's one single application in Visual Studio. Is it a correct understanding that the ASP.NET Core Hosted model splits the single app into two separate apps, Client and Server. The Client is a WebAssembly app, and it still uses SignalR to communicate with the server app for events and fetching data?

2) Between the Client and Server app, I do not see they have any relations or class references except the Server app references the Client app. I can remove the Client app reference and it still compiles. However, the site is empty. So how does the Server app knows where to load the content from the Client app? Is it just because the Client app is being referenced? Where is the "hook up"? Another reason I asked this is I already started to develop a website that used the server model, but now if I want to change to client model with ASP.NET Core Host, how do I do that?

3) In the Server app, there is _Layout.cshtml file. In the Client app, there is index.html under wwwroot. It is the index.html that gets rendered. So what is _Layout.cshtml for?

4) I saw from the sample VS template, the Server app is used as an API service (weatherforecast). In my environment, I already have a dedicated API service that I can consume. So what do I use the Server app for? Is it just an empty .NET core app to host the Client app? How do you use the Server app beyond API calls?

like image 221
Franky Avatar asked Jun 04 '20 18:06

Franky


People also ask

What is ASP.NET Core hosted in Blazor WebAssembly?

Blazor is a web framework for building web UI components (Razor components) that can be hosted in different ways. Razor components can run server-side in ASP.NET Core (Blazor Server) versus client-side in the browser on a WebAssembly-based . NET runtime (Blazor WebAssembly, Blazor WASM).

Does Blazor work with .NET Core?

Blazor Server provides support for hosting Razor components on the server in an ASP.NET Core app. UI updates are handled over a SignalR connection. The runtime stays on the server and handles: Executing the app's C# code.

What is the difference between ASP.NET Core and Blazor?

Blazor has a lot in common with ASP.NET Web Forms. Both frameworks offer component-based, event-driven, stateful UI programming models. The main architectural difference is that ASP.NET Web Forms runs only on the server. Blazor can run on the client in the browser.


1 Answers

Is it a correct understanding [...]. The Client is a WebAssembly app, and it still uses SignalR to communicate with the server app for events and fetching data?

No.

Blazor Server : Your code runs on the server and changes are pushed with SignalR.
Blazor Webassembly: Runs in the Browser, standalone.
Blazor Webassembly Hosted : is 3 projects in one solution. It's just a convenience template. The Asp.NET Server part (not a Blazor app) serves up the client and is a place to host APIs.

Blazor Wasm can use SignalR as an extra feature but it does not require it to function.

Between the Client and Server app, I do not see they have any relations or class references except the Server app references the Client app.

Correct. The server only needs that reference to find the (output) files of the Wasm app. You can remove the reference and configure something with path strings. There is no 'technical' link between the 2 projects. They run on different platforms.

In the Server app, there is _Layout.cshtml file. ... So what is _Layout.cshtml for?

It is used when you add Authorization to your project. The fact that it's there when you don't use authorization may be an oversight. To see it in action, create a Server or Hosted project with Individual accounts enabled. The Login etc pages use the _Layout.cshtml

I already have a dedicated API service that I can consume. So what do I use the Server app for?

Then you don't need it and you should just use the simple WebAssembly template. You could look into how the sample uses the Shared assembly (DTO's) and serves up the Client. You could use parts of that in your Service, or not.

like image 88
Henk Holterman Avatar answered Nov 15 '22 07:11

Henk Holterman