简体   繁体   English

了解 Blazor 装配与 ASP.net 核心托管 Model

[英]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).我已经阅读了很多关于 Blazor 和不同托管模型(实际上是 3 个)的文章。 The easiest to understand is the pure client model, which is a standalone app that runs on webassembly.最容易理解的是纯客户端 model,它是一个在 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. 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. 1) 对于服务器 model,浏览器使用 signalR 连接到服务器应用程序。 It's one single application in Visual Studio.它是 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. ASP.NET Core Hosted model 将单个应用程序拆分为两个单独的应用程序(客户端和服务器)是否正确理解。 The Client is a WebAssembly app, and it still uses SignalR to communicate with the server app for events and fetching data?客户端是一个 WebAssembly 应用程序,它仍然使用 SignalR 与服务器应用程序通信以获取事件和获取数据?

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. 2)在客户端和服务器应用程序之间,我看不到它们有任何关系或 class 引用,除了服务器应用程序引用客户端应用程序。 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?我问这个的另一个原因是我已经开始开发一个使用服务器 model 的网站,但是现在如果我想用 ASP.NET 核心主机更改为客户端 model,该怎么做?

3) In the Server app, there is _Layout.cshtml file. 3) 在服务器应用程序中,有 _Layout.cshtml 文件。 In the Client app, there is index.html under wwwroot.在客户端应用程序中,wwwroot 下有 index.html。 It is the index.html that gets rendered.渲染的是 index.html。 So what is _Layout.cshtml for?那么 _Layout.cshtml 是干什么用的呢?

4) I saw from the sample VS template, the Server app is used as an API service (weatherforecast). 4)我从示例 VS 模板中看到,Server 应用程序用作 API 服务(天气预报)。 In my environment, I already have a dedicated API service that I can consume.在我的环境中,我已经有一个可以使用的专用 API 服务。 So what do I use the Server app for?那么我将服务器应用程序用于什么? Is it just an empty .NET core app to host the Client app?托管客户端应用程序是否只是一个空的 .NET 核心应用程序? How do you use the Server app beyond API calls?除了 API 调用之外,您如何使用服务器应用程序?

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?客户端是一个 WebAssembly 应用程序,它仍然使用 SignalR 与服务器应用程序通信以获取事件和获取数据?

No.不。

Blazor Server: Your code runs on the server and changes are pushed with SignalR. Blazor 服务器:您的代码在服务器上运行并使用 SignalR 推送更改。
Blazor Webassembly: Runs in the Browser, standalone. Blazor Webassembly:在浏览器中运行,独立。
Blazor Webassembly Hosted: is 3 projects in one solution. Blazor Webassembly 托管:在一个解决方案中包含 3 个项目。 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. Asp.NET 服务器部分(不是 Blazor 应用程序)为客户端提供服务,并且是托管 API 的地方。

Blazor Wasm can use SignalR as an extra feature but it does not require it to function. Blazor Wasm 可以使用 SignalR 作为额外功能,但它不需要 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.在客户端和服务器应用程序之间,我看不到它们有任何关系或 class 引用,除了服务器应用程序引用客户端应用程序。

Correct.正确的。 The server only needs that reference to find the (output) files of the Wasm app.服务器只需要该引用来查找 Wasm 应用程序的(输出)文件。 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.在服务器应用程序中,有 _Layout.cshtml 文件。 ... So what is _Layout.cshtml for? ... 那么 _Layout.cshtml 是干什么用的?

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登录等页面使用 _Layout.cshtml

I already have a dedicated API service that I can consume.我已经有一个可以使用的专用 API 服务。 So what do I use the Server app for?那么我将服务器应用程序用于什么?

Then you don't need it and you should just use the simple WebAssembly template.那么你就不需要它了,你应该只使用简单的 WebAssembly 模板。 You could look into how the sample uses the Shared assembly (DTO's) and serves up the Client.您可以查看示例如何使用共享程序集 (DTO) 并为客户端提供服务。 You could use parts of that in your Service, or not.您可以在您的服务中使用其中的一部分,也可以不使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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