首页 > 解决方案 > 将 Blazor 组件渲染为静态 HTML 并将其作为流返回

问题描述

是否可以将 blazor 组件呈现为字符串,以便我可以呈现静态 HTML。我知道将 MVC 视图呈现为字符串的方法在哪里。我需要做的是创建一种方法来创建静态 HTML 页面,该页面将下载到 WPF 应用程序,然后用户可以在浏览器中打开它?这是 MVC 应用程序的流行答案:如何将 ASP.NET MVC 视图呈现为字符串? 那么 Blazor 有类似的东西吗?我不需要在 Blazor 中执行此操作,但 web 是在 Blazor 中完成的,所以会很好。

标签: c#asp.net-mvcblazorblazor-server-sideblazor-client-side

解决方案


来自 Blazor .Net 5 RC1 https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-5-release-candidate-1/#blazor-webassembly-prerendering

在您的托管应用程序中,您可以使用组件标签助手render-mode="Static"render-mode="WebAssemblyPrerendered"

@page "/static-counter"
@namespace Server.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<component type="typeof(Client.Shared.MyComponent)" render-mode="WebAssemblyPrerendered" />

推荐阅读