Skip to content

Commit 0ac27ac

Browse files
authored
Small article improvements (#34919)
1 parent 57446aa commit 0ac27ac

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

aspnetcore/blazor/components/render-components-outside-of-aspnetcore.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@ uid: blazor/components/render-outside-of-aspnetcore
1616

1717
In the following example, a Razor component is rendered to an HTML string from a console app:
1818

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:
2020

2121
```dotnetcli
2222
dotnet new console -o ConsoleApp1
2323
cd ConsoleApp1
2424
```
2525

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>:
2727

2828
```dotnetcli
2929
dotnet add package Microsoft.AspNetCore.Components.Web
30+
```
31+
32+
Add a package reference for <xref:Microsoft.Extensions.Logging?displayProperty=fullName>:
33+
34+
```dotnetcli
3035
dotnet add package Microsoft.Extensions.Logging
3136
```
3237

@@ -48,17 +53,15 @@ Add the following `RenderMessage` component to the project.
4853
4954
@code {
5055
[Parameter]
51-
public string Message { get; set; }
56+
public string? Message { get; set; }
5257
}
5358
```
5459

55-
Update the `Program` file:
60+
Replace the code in the `Program` file with the following code:
5661

5762
* 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>).
5863
* Create an <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer> and render the `RenderMessage` component by calling <xref:Microsoft.AspNetCore.Components.Web.HtmlRenderer.RenderComponentAsync%2A>.
5964

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-
6265
```csharp
6366
using Microsoft.AspNetCore.Components;
6467
using Microsoft.AspNetCore.Components.Web;
@@ -93,6 +96,8 @@ Console.WriteLine(html);
9396
> [!NOTE]
9497
> 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.
9598
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+
96101
Alternatively, you can write the HTML to a <xref:System.IO.TextWriter> by calling `output.WriteHtmlTo(textWriter)`.
97102

98103
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

Comments
 (0)