当我使用 .NET 8 创建一个新的 Blazor 解决方案并启用 webassembly 时,我得到了两个项目,客户端和服务器。
我可以添加组件
@rendermode InteractiveAuto
到客户端项目的 Pages/ 文件夹,它们工作正常。
当我对服务器项目执行相同操作时,我在浏览器开发人员控制台中收到此消息:
"Error: One or more errors occurred. (Root component type 'LearnBlazor.Components.Whatever' could not be found in the assembly 'LearnBlazor'"
其中 LearnBlazor 是服务器端。
我尝试在服务器项目的 Program.cs 中添加一行:
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(LearnBlazor.Client._Imports).Assembly)
.AddAdditionalAssemblies(typeof(LearnBlazor.Components.Pages.ComponentNotWorking).Assembly)
但这会引发异常(程序集已添加)。
我曾在 Pluralsight 课程(准确地说是 Blazor 基础知识)和 Microsoft Learn 上学习过 Blazor,但这两门课程都没有涵盖客户端和服务器项目。这两门课程都使用单个项目解决方案。
是的,我知道将组件添加到客户端项目可以使其工作,但我想知道它是否也可以在服务器上运行,如果可以,该如何操作。
此外,我想知道是否有任何有效的用例可以将交互式组件添加到服务器端。我对此表示怀疑,因为客户端是处理点击的人,但我还是询问了一下以确保万无一失。
1 个回答