Skip to content

Commit 0f9dadb

Browse files
guardrexDonciavas
authored andcommitted
Render components outside of ASPNETCORE (dotnet#28981)
1 parent e12e5f6 commit 0f9dadb

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
title: Render Razor components outside of ASP.NET Core
3+
author: guardrex
4+
description: Render Razor components outside of the context of an HTTP request.
5+
monikerRange: '>= aspnetcore-8.0'
6+
ms.author: riande
7+
ms.custom: mvc
8+
ms.date: 04/20/2023
9+
uid: blazor/components/render-outside-of-aspnetcore
10+
---
11+
# Render Razor components outside of ASP.NET Core
12+
13+
Razor components can be rendered outside of the context of an HTTP request. You can render Razor components as HTML directly to a string or stream independently of the ASP.NET Core hosting environment. This is convenient for scenarios where you want to generate HTML fragments, such as for generating email content, generating static site content, or for building a content templating engine.
14+
15+
In the following example, a Razor component is rendered to an HTML string from a console app:
16+
17+
In a command shell, create a new console app project:
18+
19+
```dotnetcli
20+
dotnet new console -o ConsoleApp1
21+
cd ConsoleApp1
22+
```
23+
24+
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:
25+
26+
```dotnetcli
27+
dotnet add package Microsoft.AspNetCore.Components.Web --prerelease
28+
dotnet add package Microsoft.Extensions.Logging --prerelease
29+
```
30+
31+
In the console app's project file (`ConsoleApp1.csproj`), update the console app project to use the Razor SDK:
32+
33+
```diff
34+
- <Project Sdk="Microsoft.NET.Sdk">
35+
+ <Project Sdk="Microsoft.NET.Sdk.Razor">
36+
```
37+
38+
In a command shell, add a Razor component to the project:
39+
40+
```dotnetcli
41+
dotnet new razorcomponent -n Component1
42+
```
43+
44+
```razor
45+
<h1>Component1</h1>
46+
47+
<p>Hello from Component1!</p>
48+
49+
@code {
50+
51+
}
52+
```
53+
54+
Update `Program.cs`:
55+
56+
* 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>).
57+
* Create an `HtmlRenderer` and render the `Component1` component by calling `RenderComponentAsync`.
58+
59+
Any calls to `RenderComponentAsync` must be made in the context of calling `InvokeAsync` on a component dispatcher. A component dispatcher is available from the `HtmlRender.Dispatcher` property.
60+
61+
```csharp
62+
using Microsoft.AspNetCore.Components;
63+
using Microsoft.AspNetCore.Components.Web;
64+
using Microsoft.Extensions.DependencyInjection;
65+
using Microsoft.Extensions.Logging;
66+
using ConsoleApp1;
67+
68+
IServiceCollection services = new ServiceCollection();
69+
services.AddLogging();
70+
71+
IServiceProvider serviceProvider = services.BuildServiceProvider();
72+
ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
73+
74+
await using var htmlRenderer = new HtmlRenderer(serviceProvider, loggerFactory);
75+
76+
var html = await htmlRenderer.Dispatcher.InvokeAsync(async () =>
77+
{
78+
var parameters = ParameterView.Empty;
79+
var output = await htmlRenderer.RenderComponentAsync<Component1>(parameters);
80+
return output.ToHtmlString();
81+
});
82+
83+
Console.WriteLine(html);
84+
```
85+
86+
Alternatively, you can write the HTML to a <xref:System.IO.TextWriter> by calling `output.WriteHtmlTo(textWriter)`.
87+
88+
The task returned by `RenderComponentAsync` completes when the component is fully rendered, including completing any asynchronous lifecycle methods. If you want to observe the rendered HTML earlier, call `BeginRenderComponentAsync` instead. Then, wait for the component rendering to complete by awaiting `WaitForQuiescenceAsync` on the returned `HtmlComponent` instance.

aspnetcore/toc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ items:
463463
uid: blazor/components/class-libraries
464464
- name: JavaScript apps and SPA frameworks
465465
uid: blazor/components/js-spa-frameworks
466+
- name: Components outside of ASP.NET Core
467+
uid: blazor/components/render-outside-of-aspnetcore
466468
- name: Built-in components
467469
uid: blazor/components/built-in-components
468470
- name: Globalization and localization

0 commit comments

Comments
 (0)