实际上,ASP.NET Core Hosted和服务器端Blazor有什么区别? [英] What's the difference between ASP.NET Core Hosted and Server-Side Blazor, really?

查看:501
本文介绍了实际上,ASP.NET Core Hosted和服务器端Blazor有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在努力理解 ASP.NET Core托管服务器端 Blazor之间的区别.我已经知道相同的问题存在,但这并不令人满意.实际上,我在任何地方都找不到令人满意的答案-答案大致相同.

I'm still struggling to understand the difference between ASP.NET Core Hosted and Server-side Blazor. I know same question already exists, but it's not satisfying. In fact, I couldn't find the satisfying answer anywhere - the answers were more or less the same.

如果 hosted 选项使用服务器(IIS,Kestrel),那么为什么服务器端?令人困惑...官方文档也没有阐明这一点真是可惜...

If hosted option uses server (IIS, Kestrel), then why server-side? Confusing... It's a shame that official documentation didn't shed the light either...

混乱的原因是我们有三个选项来创建Blazor应用程序.执行dotnew new --list后,我得到:

The confusion stems from the fact that we have THREE options to create Blazor application. After executing dotnew new --list I get:

  1. dotnet new blazorserver(Blazor服务器应用程序)

  1. dotnet new blazorserver (Blazor Server App)

dotnet blazorwasm(Blazor WebAssembly应用)

dotnet blazorwasm (Blazor WebAssembly App)

但是,还有第三种选择:

However, there's a third option:

  1. dotnet blazorwasm --hosted(或dotnet blazor --hosted)

与创建应用程序时Visual Studio中的复选框相同:

It's the same as check box in Visual Studio when creating application:

文档说:

您可以选择将应用程序配置为使用ASP.NET Core 后端,方法是选中"ASP.NET Core托管"复选框

you have the option of configuring the app to use an ASP.NET Core backend by selecting the ASP.NET Core hosted check box

但是没有提供解释是什么意思...

But no explanation was provided what does it mean...

推荐答案

它们是两个托管模型:服务器托管和客户端托管.

They are two hosting models: server-hosted, and client-hosted.

区别在于该应用程序是托管在服务器还是客户端中.服务器托管意味着您的应用程序逻辑在服务器中运行(您可以认为它类似于Web窗体),单击按钮,"Ajax"调用发送请求,服务器接收请求,并将更新后的内容发回页.但是,这里使用的是SignalR而不是Ajax,这是一种低级套接字通信(读取效率高).而不是更新整个页面,它仅更新相关部分(因此它是单页面应用程序).

The difference is whether the app is hosted in server, or in client. Server hosting means your app logic runs in the server (you can think of it similar to what Web Forms is), you click on a button, an "Ajax" call sends the request, the server receives the request, and sends back the updated page. However, here it uses SignalR not Ajax, which is a low level socket communication (read efficient). And instead of updating a whole page, it updates only the relevant parts (thus it is a single page application).

另一方面,客户端托管意味着您的逻辑在浏览器中运行.可以将其视为您的C#逻辑已转换为JS,并将其嵌入在页面中.因此,逻辑在浏览器中运行.在您可能要阅读的WebAssembly引入之后,这是可能的.

On the other hand, client hosting means your logic runs within the browser. Think of it as if your C# logic is converted into JS, and it is embedded in the page. So the logic runs in the browser. This is possible after the introduction of WebAssembly which you might want to read about.

假设您要创建一个计算器应用.然后,您的服务器托管的应用程序将需要与服务器通信以计算并获取每次计算的结果,而托管的客户端则不需要,并在浏览器中计算结果.

Let's say you want to create a calculator app. Your server hosted app will then need to communicate with the server to calculate and get the result for each calculation, while the client hosted does not need, and calculates the result in browser.

您可能想知道,为什么我们有两个选择.原因是在许多浏览器中对WebAssembly的支持(客户端托管的应用程序所依赖)不完整或不存在,并且性能也存在很大差异.

You might wonder, why we have two options. The reason being that support for WebAssembly (which a client hosted app relies on) is either incomplete or non-existant in many browsers, and performance differs widely too.

https://caniuse.com/#feat=wasm

更新:

我知道你现在从哪里来.造成这种混乱的原因是,使用客户端托管的Blazor时您有一个名为--hosted的选项.此选项意味着让Blazor包含ASP.NET Core运行时.

I see where you are coming from now. The confusion stems from the fact that you have an option called --hosted when using the client-hosted Blazor. This options means having Blazor to include ASP.NET Core runtime.

为什么选择此选项?因为您可以编写不需要与外部服务进行任何类型连接的脱机应用程序(例如计算器应用程序),所以使ASP.NET Core无关紧要.但是,您可能想编写一个在线应用程序,以访问在线数据库,外部API,进行验证等.对于这类应用程序,您将需要一个ASP.NET Core堆栈来支持您的应用程序.

Why this option? Because you can write an offline app (e.g. calculator app) that does not need any kind of connection to external services, making ASP.NET Core irrelevant. However, you might want to write an online app that accesses online DB, external APIs, do verification, etc. For these kind of apps, you will need an ASP.NET Core stack to support your app.

检查此常见问题解答: https://github.com/aspnet/Blazor/wiki/FAQ#q-can-i-use-blazor-with-aspnet-core-on-the-server

Check this FAQ: https://github.com/aspnet/Blazor/wiki/FAQ#q-can-i-use-blazor-with-aspnet-core-on-the-server

这篇关于实际上,ASP.NET Core Hosted和服务器端Blazor有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆