You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the following example, a Razor component is rendered to an HTML string from a console app:
18
18
19
-
In a command shell, create a new console app project:
19
+
In a command shell, create a new console app project and change the directory to the `ConsoleApp1` folder:
20
20
21
21
```dotnetcli
22
22
dotnet new console -o ConsoleApp1
23
23
cd ConsoleApp1
24
24
```
25
25
26
-
In a command shell in the `ConsoleApp1` folder, add package references for <xref:Microsoft.AspNetCore.Components.Web?displayProperty=fullName> and <xref:Microsoft.Extensions.Logging?displayProperty=fullName> to the console app:
26
+
Add a package reference for <xref:Microsoft.AspNetCore.Components.Web?displayProperty=fullName>:
Add a package reference for <xref:Microsoft.Extensions.Logging?displayProperty=fullName>:
33
+
34
+
```dotnetcli
30
35
dotnet add package Microsoft.Extensions.Logging
31
36
```
32
37
@@ -48,17 +53,15 @@ Add the following `RenderMessage` component to the project.
48
53
49
54
@code {
50
55
[Parameter]
51
-
public string Message { get; set; }
56
+
public string? Message { get; set; }
52
57
}
53
58
```
54
59
55
-
Update the `Program` file:
60
+
Replace the code in the `Program` file with the following code:
56
61
57
62
* Set up dependency injection (<xref:Microsoft.Extensions.DependencyInjection.IServiceCollection>/<xref:Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider%2A>) and logging (<xref:Microsoft.Extensions.DependencyInjection.LoggingServiceCollectionExtensions.AddLogging%2A>/<xref:Microsoft.Extensions.Logging.ILoggerFactory>).
58
63
* Create an <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer> and render the `RenderMessage` component by calling <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer.RenderComponentAsync%2A>.
59
64
60
-
Any calls to <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer.RenderComponentAsync%2A> must be made in the context of calling `InvokeAsync` on a component dispatcher. A component dispatcher is available from the <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer.Dispatcher?displayProperty=nameWithType> property.
61
-
62
65
```csharp
63
66
usingMicrosoft.AspNetCore.Components;
64
67
usingMicrosoft.AspNetCore.Components.Web;
@@ -93,6 +96,8 @@ Console.WriteLine(html);
93
96
> [!NOTE]
94
97
> Pass <xref:Microsoft.AspNetCore.Components.ParameterView.Empty?displayProperty=nameWithType> to <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer.RenderComponentAsync%2A> when rendering the component without passing parameters.
95
98
99
+
Any calls to <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer.RenderComponentAsync%2A> must be made in the context of calling `InvokeAsync` on a component dispatcher. A component dispatcher is available from the <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer.Dispatcher?displayProperty=nameWithType> property.
100
+
96
101
Alternatively, you can write the HTML to a <xref:System.IO.TextWriter> by calling `output.WriteHtmlTo(textWriter)`.
97
102
98
103
The task returned by <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer.RenderComponentAsync%2A> completes when the component is fully rendered, including completing any asynchronous lifecycle methods. If you want to observe the rendered HTML earlier, call <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer.BeginRenderingComponent%2A> instead. Then, wait for the component rendering to complete by awaiting <xref:Microsoft.AspNetCore.Components.Web.HtmlRendering.HtmlRootComponent.QuiescenceTask%2A?displayProperty=nameWithType> on the returned <xref:Microsoft.AspNetCore.Components.Web.HtmlRendering.HtmlRootComponent> instance.
0 commit comments