Skip to content

Commit b73119a

Browse files
authored
Component TH topic (#17343)
1 parent 9f204c3 commit b73119a

File tree

6 files changed

+102
-28
lines changed

6 files changed

+102
-28
lines changed

.ghal.rules.json

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
"(?i).*master\/aspnetcore\/host-and-deploy\/blazor.*": {
1919
"labels-add": "Blazor"
2020
},
21+
"(?i).*master\/aspnetcore\/mvc\/views\/tag-helpers\/built-in\/component-tag-helper.md": {
22+
"labels-add": "Blazor"
23+
},
2124
"(?i).*master\/aspnetcore\/security\/blazor.*": {
2225
"labels-add": "Blazor"
2326
},

aspnetcore/blazor/handle-errors.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Discover how ASP.NET Core Blazor how Blazor manages unhandled excep
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 02/19/2020
8+
ms.date: 03/17/2020
99
no-loc: [Blazor, SignalR]
1010
uid: blazor/handle-errors
1111
---
@@ -187,7 +187,7 @@ For more information, see the following articles:
187187

188188
### Blazor Server prerendering
189189

190-
Blazor components can be prerendered using the `Component` Tag Helper so that their rendered HTML markup is returned as part of the user's initial HTTP request. This works by:
190+
Blazor components can be prerendered using the [Component Tag Helper](xref:mvc/views/tag-helpers/builtin-th/component-tag-helper) so that their rendered HTML markup is returned as part of the user's initial HTTP request. This works by:
191191

192192
* Creating a new circuit for all of the prerendered components that are part of the same page.
193193
* Generating the initial HTML.

aspnetcore/blazor/integrate-components.md

+3-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn about data binding scenarios for components and DOM elements
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 02/12/2020
8+
ms.date: 03/17/2020
99
no-loc: [Blazor, SignalR]
1010
uid: blazor/integrate-components
1111
---
@@ -217,31 +217,10 @@ For more information, see <xref:blazor/components#import-components>.
217217

218218
*This section pertains to adding components to pages or views, where the components aren't directly routable from user requests.*
219219

220-
To render a component from a page or view, use the `Component` Tag Helper:
221-
222-
```cshtml
223-
<component type="typeof(Counter)" render-mode="ServerPrerendered"
224-
param-IncrementAmount="10" />
225-
```
226-
227-
The parameter type must be JSON serializable, which typically means that the type must have a default constructor and settable properties. For example, you can specify a value for `IncrementAmount` because the type of `IncrementAmount` is an `int`, which is a primitive type supported by the JSON serializer.
228-
229-
`RenderMode` configures whether the component:
230-
231-
* Is prerendered into the page.
232-
* Is rendered as static HTML on the page or if it includes the necessary information to bootstrap a Blazor app from the user agent.
233-
234-
| `RenderMode` | Description |
235-
| ------------------- | ----------- |
236-
| `ServerPrerendered` | Renders the component into static HTML and includes a marker for a Blazor Server app. When the user-agent starts, this marker is used to bootstrap a Blazor app. |
237-
| `Server` | Renders a marker for a Blazor Server app. Output from the component isn't included. When the user-agent starts, this marker is used to bootstrap a Blazor app. |
238-
| `Static` | Renders the component into static HTML. |
239-
240-
While pages and views can use components, the converse isn't true. Components can't use view- and page-specific scenarios, such as partial views and sections. To use logic from partial view in a component, factor out the partial view logic into a component.
241-
242-
Rendering server components from a static HTML page isn't supported.
220+
To render a component from a page or view, use the [Component Tag Helper](xref:mvc/views/tag-helpers/builtin-th/component-tag-helper).
243221

244222
For more information on how components are rendered, component state, and the `Component` Tag Helper, see the following articles:
245223

246224
* <xref:blazor/hosting-models>
247225
* <xref:blazor/hosting-model-configuration>
226+
* <xref:mvc/views/tag-helpers/builtin-th/component-tag-helper>

aspnetcore/blazor/state-management.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to persist state in Blazor Server apps.
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 12/18/2019
8+
ms.date: 03/17/2020
99
no-loc: [Blazor, SignalR]
1010
uid: blazor/state-management
1111
---
@@ -234,7 +234,7 @@ During prerendering:
234234
235235
One way to resolve the error is to disable prerendering. This is usually the best choice if the app makes heavy use of browser-based storage. Prerendering adds complexity and doesn't benefit the app because the app can't prerender any useful content until `localStorage` or `sessionStorage` are available.
236236

237-
To disable prerendering, open the *Pages/_Host.cshtml* file and change the call to `render-mode` of the `Component` Tag Helper to `Server`.
237+
To disable prerendering, open the *Pages/_Host.cshtml* file and change the `render-mode` of the [Component Tag Helper](xref:mvc/views/tag-helpers/builtin-th/component-tag-helper) to <xref:Microsoft.AspNetCore.Mvc.Rendering.RenderMode.Server>.
238238

239239
Prerendering might be useful for other pages that don't use `localStorage` or `sessionStorage`. To keep prerendering enabled, defer the loading operation until the browser is connected to the circuit. The following is an example for storing a counter value:
240240

aspnetcore/includes/built-in-TH.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
**[Cache Tag Helper](xref:mvc/views/tag-helpers/builtin-th/cache-tag-helper)**
66

7+
**[Component Tag Helper](xref:mvc/views/tag-helpers/builtin-th/component-tag-helper)**
8+
79
**[Distributed Cache Tag Helper](xref:mvc/views/tag-helpers/builtin-th/distributed-cache-tag-helper)**
810

911
**[Environment Tag Helper](xref:mvc/views/tag-helpers/builtin-th/environment-tag-helper)**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: Component Tag Helper in ASP.NET Core
3+
author: guardrex
4+
ms.author: riande
5+
description: Learn how to use the ASP.NET Core Component Tag Helper to render Razor components in pages and views.
6+
ms.custom: mvc
7+
ms.date: 03/18/2020
8+
no-loc: [Blazor, SignalR]
9+
uid: mvc/views/tag-helpers/builtin-th/component-tag-helper
10+
---
11+
# Component Tag Helper in ASP.NET Core
12+
13+
By [Daniel Roth](https://github.com/danroth27) and [Luke Latham](https://github.com/guardrex)
14+
15+
To render a component from a page or view, use the [Component Tag Helper](xref:Microsoft.AspNetCore.Mvc.TagHelpers.ComponentTagHelper).
16+
17+
The following Component Tag Helper renders the `Counter` component in a page or view:
18+
19+
```cshtml
20+
<component type="typeof(Counter)" render-mode="ServerPrerendered" />
21+
```
22+
23+
The Component Tag Helper can also pass parameters to components. Consider the following `ColorfulCheckbox` component that sets the check box label's color and size:
24+
25+
```razor
26+
<label style="font-size:@(Size)px;color:@Color">
27+
<input @bind="Value"
28+
id="survey"
29+
name="blazor"
30+
type="checkbox" />
31+
Enjoying Blazor?
32+
</label>
33+
34+
@code {
35+
[Parameter]
36+
public bool Value { get; set; }
37+
38+
[Parameter]
39+
public int Size { get; set; } = 8;
40+
41+
[Parameter]
42+
public string Color { get; set; }
43+
44+
protected override void OnInitialized()
45+
{
46+
Size += 10;
47+
}
48+
}
49+
```
50+
51+
The `Size` (`int`) and `Color` (`string`) [component parameters](xref:blazor/components#component-parameters) can be set by the Component Tag Helper:
52+
53+
```cshtml
54+
<component type="typeof(ColorfulCheckbox)" render-mode="ServerPrerendered"
55+
param-Size="14" param-Color="@("blue")" />
56+
```
57+
58+
The following HTML is rendered in the page or view:
59+
60+
```html
61+
<label style="font-size:24px;color:blue">
62+
<input id="survey" name="blazor" type="checkbox">
63+
Enjoying Blazor?
64+
</label>
65+
```
66+
67+
Passing a quoted string requires an [explicit Razor expression](xref:mvc/views/razor#explicit-razor-expressions), as shown for `param-Color` in the preceding example. The Razor parsing behavior for a `string` type value doesn't apply to a `param-*` attribute because the attribute is an `object` type.
68+
69+
The parameter type must be JSON serializable, which typically means that the type must have a default constructor and settable properties. For example, you can specify a value for `Size` and `Color` in the preceding example because the types of `Size` and `Color` are primitive types (`int` and `string`), which are supported by the JSON serializer.
70+
71+
<xref:Microsoft.AspNetCore.Mvc.Rendering.RenderMode> configures whether the component:
72+
73+
* Is prerendered into the page.
74+
* Is rendered as static HTML on the page or if it includes the necessary information to bootstrap a Blazor app from the user agent.
75+
76+
| Render Mode | Description |
77+
| ----------- | ----------- |
78+
| <xref:Microsoft.AspNetCore.Mvc.Rendering.RenderMode.ServerPrerendered> | Renders the component into static HTML and includes a marker for a Blazor Server app. When the user-agent starts, this marker is used to bootstrap a Blazor app. |
79+
| <xref:Microsoft.AspNetCore.Mvc.Rendering.RenderMode.Server> | Renders a marker for a Blazor Server app. Output from the component isn't included. When the user-agent starts, this marker is used to bootstrap a Blazor app. |
80+
| <xref:Microsoft.AspNetCore.Mvc.Rendering.RenderMode.Static> | Renders the component into static HTML. |
81+
82+
While pages and views can use components, the converse isn't true. Components can't use view- and page-specific features, such as partial views and sections. To use logic from a partial view in a component, factor out the partial view logic into a component.
83+
84+
Rendering server components from a static HTML page isn't supported.
85+
86+
## Additional resources
87+
88+
* <xref:Microsoft.AspNetCore.Mvc.TagHelpers.ComponentTagHelper>
89+
* <xref:mvc/views/tag-helpers/intro>
90+
* <xref:blazor/components>

0 commit comments

Comments
 (0)