组件标记帮助程序用于在 Razor Pages 页面或 MVC 视图中渲染 Razor 组件。
先决条件
按照 将 ASP.NET Core Razor 组件与 MVC 或 Razor Pages 集成 一文中 在页面或视图中使用不可路由组件 部分的指导进行操作。
对于以下任一情况,请遵循“配置”部分中的指南:
- Blazor Server:在 Razor Pages 和 MVC 应用中集成可路由和不可路由的 Razor 组件。
- Blazor WebAssembly:将托管的 Blazor WebAssembly 解决方案中的 Razor 组件集成到 Razor Pages 和 MVC 应用中。
按照《将 ASP.NET Core Razor组件与 MVC 或 RazorPages 集成》一文中配置部分的指南进行操作。
组件标记帮助程序
若要在页面或视图中呈现组件,请使用组件标记帮助程序(<component> 标记)。
注意
在 .NET 5 或更高版本的 ASP.NET Core 中,支持将Razor组件集成到Razor页面和 MVC 应用中的托管Blazor WebAssembly应用里。
RenderMode 配置组件是否:
- 被预渲染到页面中。
- 在页面上呈现为静态 HTML,或者包含从用户智能体启动 Blazor 应用所需的信息。
下表列出了 Blazor WebAssembly 应用呈现模式。
| 呈现模式 | 描述 |
|---|---|
WebAssembly |
呈现 Blazor WebAssembly 应用程序的标记,以便在浏览器中进行加载时包含交互式组件。 该组件未预渲染。 使用此选项,可以更轻松地在不同页面上渲染不同的 Blazor WebAssembly 组件。 |
WebAssemblyPrerendered |
将组件预渲染为静态 HTML,并包含一个用于 Blazor WebAssembly 应用的标记,以便在浏览器加载后使该组件可交互。 |
渲染模式如下表所示。
| 呈现模式 | 描述 |
|---|---|
| ServerPrerendered | 在静态 HTML 中呈现组件,并包含服务器端 Blazor 应用的标记。 当用户代理启动时,此标记用于引导启动 Blazor 应用程序。 |
| Server | 呈现服务器端 Blazor 应用的标记。 不包括组件的输出。 当用户代理启动时,此标记用于引导启动 Blazor 应用程序。 |
| Static | 将组件渲染为静态 HTML。 |
应用渲染模式如下表所示。
| 呈现模式 | 描述 |
|---|---|
| ServerPrerendered | 在静态 HTML 中呈现组件,并包含服务器端 Blazor 应用的标记。 当用户代理启动时,此标记用于引导启动 Blazor 应用程序。 |
| Server | 呈现服务器端 Blazor 应用的标记。 不包括组件的输出。 当用户代理启动时,此标记用于引导启动 Blazor 应用程序。 |
| Static | 将组件渲染为静态 HTML。 |
其他特征包括:
- 允许多个组件标记帮助程序呈现多个 Razor 组件。
- 应用启动后,无法动态呈现组件。
- 尽管页面和视图可以使用组件,但反过来则不行。 组件无法使用特定于视图和页面的功能,例如分部视图和部分。 若要在组件中使用分部视图中的逻辑,请将该逻辑提取到组件中。
- 不支持从静态 HTML 页面呈现服务器组件。
以下组件标记帮助程序在使用 ServerPrerendered 的服务器端 Blazor 应用的页面或视图中呈现 EmbeddedCounter 组件:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using {APP ASSEMBLY}.Components
...
<component type="typeof(EmbeddedCounter)" render-mode="ServerPrerendered" />
上述示例假定 EmbeddedCounter 组件位于应用的 Components 文件夹中。 占位符 {APP ASSEMBLY} 是应用的程序集名称(例如 @using BlazorSample.Components)。
组件标记帮助程序还可以向组件传递参数。 以下面设置复选框标签颜色和大小的 ColorfulCheckbox 组件为例。
Components/ColorfulCheckbox.razor:
<label style="font-size:@(Size)px;color:@Color">
<input @bind="Value"
id="survey"
name="blazor"
type="checkbox" />
Enjoying Blazor?
</label>
@code {
[Parameter]
public bool Value { get; set; }
[Parameter]
public int Size { get; set; } = 8;
[Parameter]
public string? Color { get; set; }
protected override void OnInitialized()
{
Size += 10;
}
}
Size (int) 和 Color (string) 组件参数可由组件标记帮助程序设置:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using {APP ASSEMBLY}.Components
...
<component type="typeof(ColorfulCheckbox)" render-mode="ServerPrerendered"
param-Size="14" param-Color="@("blue")" />
上述示例假定 ColorfulCheckbox 组件位于 Components 文件夹中。 占位符 {APP ASSEMBLY} 是应用的程序集名称(例如 @using BlazorSample.Components)。
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using {APP ASSEMBLY}.Shared
...
<component type="typeof(EmbeddedCounter)" render-mode="ServerPrerendered" />
上述示例假定 EmbeddedCounter 组件位于应用的 Shared 文件夹中。 占位符 {APP ASSEMBLY} 是应用的程序集名称(例如,托管 Blazor 解决方案中的 @using BlazorSample.Shared 或 @using BlazorSample.Client.Shared)。
组件标记帮助程序还可以向组件传递参数。 以下面设置复选框标签颜色和大小的 ColorfulCheckbox 组件为例。
Shared/ColorfulCheckbox.razor:
<label style="font-size:@(Size)px;color:@Color">
<input @bind="Value"
id="survey"
name="blazor"
type="checkbox" />
Enjoying Blazor?
</label>
@code {
[Parameter]
public bool Value { get; set; }
[Parameter]
public int Size { get; set; } = 8;
[Parameter]
public string? Color { get; set; }
protected override void OnInitialized()
{
Size += 10;
}
}
Size (int) 和 Color (string) 组件参数可由组件标记帮助程序设置:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using {APP ASSEMBLY}.Shared
...
<component type="typeof(ColorfulCheckbox)" render-mode="ServerPrerendered"
param-Size="14" param-Color="@("blue")" />
上述示例假定 ColorfulCheckbox 组件位于 Shared 文件夹中。 占位符 {APP ASSEMBLY} 是应用的程序集名称(例如 @using BlazorSample.Shared)。
以下 HTML 在页面或视图中呈现:
<label style="font-size:24px;color:blue">
<input id="survey" name="blazor" type="checkbox">
Enjoying Blazor?
</label>
传递带引号的字符串需要一个显式 Razor 表达式,如前面示例中的 param-Color 所示。
Razor 针对 string 类型值的分析行为不适用于 param-* 属性,因为该属性是 object 类型。
支持所有类型的参数,但以下参数除外:
- 泛型参数。
- 不可序列化的参数。
- 集合参数中的继承。
- 其类型定义在 Blazor WebAssembly 应用之外或延迟加载的程序集中的参数。
- 用于接收
RenderFragment子内容的委托(例如param-ChildContent="...")。 对于这种情况,我们建议创建一个 Razor 组件 (.razor),使其引用要呈现的组件以及要传递的子内容,然后通过组件标记帮助程序在页面或视图中调用 Razor 组件。
参数类型必须是 JSON 可序列化的,这通常意味着类型必须具有默认构造函数和可设置的属性。 例如,你可以在前面的示例中为 Size 和 Color 指定一个值,因为 Size 和 Color 的类型是基元类型(int 和 string),受 JSON 序列化程序支持。
在以下示例中,一个类对象被传递给组件:
MyClass.cs:
public class MyClass
{
public MyClass()
{
}
public int MyInt { get; set; } = 999;
public string MyString { get; set; } = "Initial value";
}
类必须具有一个无参数的公共构造函数。
Components/ParameterComponent.razor:
<h2>ParameterComponent</h2>
<p>Int: @MyObject?.MyInt</p>
<p>String: @MyObject?.MyString</p>
@code
{
[Parameter]
public MyClass? MyObject { get; set; }
}
Pages/MyPage.cshtml:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using {APP ASSEMBLY}
@using {APP ASSEMBLY}.Components
...
@{
var myObject = new MyClass();
myObject.MyInt = 7;
myObject.MyString = "Set by MyPage";
}
<component type="typeof(ParameterComponent)" render-mode="ServerPrerendered"
param-MyObject="@myObject" />
上述示例假定 ParameterComponent 组件位于应用的 Components 文件夹中。 占位符 {APP ASSEMBLY} 是应用的程序集名称(例如 @using BlazorSample 和 @using BlazorSample.Components)。
MyClass 在应用的命名空间中。
Shared/ParameterComponent.razor:
<h2>ParameterComponent</h2>
<p>Int: @MyObject?.MyInt</p>
<p>String: @MyObject?.MyString</p>
@code
{
[Parameter]
public MyClass? MyObject { get; set; }
}
Pages/MyPage.cshtml:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using {APP ASSEMBLY}
@using {APP ASSEMBLY}.Shared
...
@{
var myObject = new MyClass();
myObject.MyInt = 7;
myObject.MyString = "Set by MyPage";
}
<component type="typeof(ParameterComponent)" render-mode="ServerPrerendered"
param-MyObject="@myObject" />
上述示例假定 ParameterComponent 组件位于应用的 Shared 文件夹中。 占位符 {APP ASSEMBLY} 是应用的程序集名称(例如 @using BlazorSample 和 @using BlazorSample.Shared)。
MyClass 在应用的命名空间中。