简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM