From 67e932bef834a30495f3d8fc1399c8985cb3e144 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Jul 2026 15:34:11 +0000 Subject: [PATCH 1/6] Initial plan From bd25d8027eac87af6fac5a9f7b2e2b8091e7e5ef Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Jul 2026 15:36:35 +0000 Subject: [PATCH 2/6] Create article: Return Razor component HTML from a web API in ASP.NET Core Blazor Fixes #37404 - Add new article aspnetcore/blazor/components/return-component-html-from-a-web-api.md - Add TOC entry in aspnetcore/toc.yml after 'Class libraries with static SSR' --- .../return-component-html-from-a-web-api.md | 96 +++++++++++++++++++ aspnetcore/toc.yml | 2 + 2 files changed, 98 insertions(+) create mode 100644 aspnetcore/blazor/components/return-component-html-from-a-web-api.md diff --git a/aspnetcore/blazor/components/return-component-html-from-a-web-api.md b/aspnetcore/blazor/components/return-component-html-from-a-web-api.md new file mode 100644 index 000000000000..c783b144a7f5 --- /dev/null +++ b/aspnetcore/blazor/components/return-component-html-from-a-web-api.md @@ -0,0 +1,96 @@ +--- +title: Return Razor component HTML from a web API in ASP.NET Core Blazor +ai-usage: ai-assisted +author: guardrex +description: Learn how to return HTML (as a string) from a web API (Minimal API) using the RazorComponentResult class. +monikerRange: '>= aspnetcore-6.0' +ms.author: wpickett +ms.date: 07/31/2026 +uid: blazor/components/return-component-html-from-a-web-api +--- +# Return Razor component HTML from a web API in ASP.NET Core Blazor + +[!INCLUDE[](~/includes/not-latest-version.md)] + +Components returned via are rendered as static HTML strings. Features requiring an active SignalR connection for component interactivity can't execute in this context. + +## Place the Razor components into the web API + +`Components/Greeting.razor`: + +```razor +

Greeting component

+ +

Hello, world!

+``` + +`Components/UserData.razor`: + +```razor +

UserData component

+ +

Viewing details for User ID: @UserId

+ +@code { + [Parameter] + public int UserId { get; set; } +} +``` + +## Add Razor component services and endpoints to the web API + +For components with parameters, pass dictionary keys that exactly match the names of properties decorated with `[Parameter]` on the component. + +```csharp +using Microsoft.AspNetCore.Http.HttpResults; +using MinimalAPI.Components; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddRazorComponents(); + +var app = builder.Build(); + +app.MapGet("/hello", () => new RazorComponentResult()); + +app.MapGet("/user/{id:int}", (int id) => +{ + var parameters = new Dictionary + { + { "UserId", id } + }; + + return new RazorComponentResult(parameters); +}); +``` + +## Consume the API and read the component HTML from the `RazorComponentResult` + +In the app's `Program` file, set up HTTP client services. The following example assumes that the web API responds to requests at `https://localhost:7286` using a [named `HttpClient`](xref:blazor/call-web-api#named-httpclient-with-ihttpclientfactory): + +```csharp +builder.Services.AddHttpClient("WebAPI", client => + client.BaseAddress = new Uri("https://localhost:7286")); +``` + +In a component, create an and request the component HTML (as `string`s), casting each to a for rendering: + +```razor +@inject IHttpClientFactory ClientFactory + +@greeting + +@userData + +@code { + private MarkupString? greeting; + private MarkupString? userData; + + protected override async Task OnInitializedAsync() + { + var client = ClientFactory.CreateClient("WebAPI"); + greeting = new MarkupString(await client.GetStringAsync("hello")); + userData = new MarkupString(await client.GetStringAsync("user/12345")); + } +} +``` diff --git a/aspnetcore/toc.yml b/aspnetcore/toc.yml index 2b39f43589e2..c50728f3c913 100644 --- a/aspnetcore/toc.yml +++ b/aspnetcore/toc.yml @@ -838,6 +838,8 @@ items: uid: blazor/components/class-libraries - name: Class libraries with static SSR uid: blazor/components/class-libraries-with-static-ssr + - name: Return a component from a web API + uid: blazor/components/return-component-html-from-a-web-api - name: JavaScript apps and SPA frameworks uid: blazor/components/js-spa-frameworks - name: Components outside of ASP.NET Core From 3dc48c38a89ad0bd6358c8ab0fd9b706f78c13a5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Jul 2026 15:47:02 +0000 Subject: [PATCH 3/6] Apply PR suggestions: update intro paragraph and [Parameter] attribute link --- .../components/return-component-html-from-a-web-api.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aspnetcore/blazor/components/return-component-html-from-a-web-api.md b/aspnetcore/blazor/components/return-component-html-from-a-web-api.md index c783b144a7f5..db1247ab6c54 100644 --- a/aspnetcore/blazor/components/return-component-html-from-a-web-api.md +++ b/aspnetcore/blazor/components/return-component-html-from-a-web-api.md @@ -12,7 +12,9 @@ uid: blazor/components/return-component-html-from-a-web-api [!INCLUDE[](~/includes/not-latest-version.md)] -Components returned via are rendered as static HTML strings. Features requiring an active SignalR connection for component interactivity can't execute in this context. +This article demonstrates how to return Razor component HTML (as a `string`) from a web API (Minimal API) using the class. + +Components returned via are rendered as static HTML strings. Features requiring an active SignalR connection for component interactivity can't execute in this context. ## Place the Razor components into the web API @@ -39,7 +41,7 @@ Components returned via Date: Fri, 31 Jul 2026 15:59:15 +0000 Subject: [PATCH 4/6] Apply reviewer suggestions: non-generic RazorComponentResult xref and MarkupString security warning --- .../components/return-component-html-from-a-web-api.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/aspnetcore/blazor/components/return-component-html-from-a-web-api.md b/aspnetcore/blazor/components/return-component-html-from-a-web-api.md index db1247ab6c54..60de496459f9 100644 --- a/aspnetcore/blazor/components/return-component-html-from-a-web-api.md +++ b/aspnetcore/blazor/components/return-component-html-from-a-web-api.md @@ -12,9 +12,9 @@ uid: blazor/components/return-component-html-from-a-web-api [!INCLUDE[](~/includes/not-latest-version.md)] -This article demonstrates how to return Razor component HTML (as a `string`) from a web API (Minimal API) using the class. +This article demonstrates how to return Razor component HTML (as a `string`) from a web API (Minimal API) using the class. -Components returned via are rendered as static HTML strings. Features requiring an active SignalR connection for component interactivity can't execute in this context. +Components returned via are rendered as static HTML strings. Features requiring an active SignalR connection for component interactivity can't execute in this context. ## Place the Razor components into the web API @@ -75,7 +75,10 @@ builder.Services.AddHttpClient("WebAPI", client => client.BaseAddress = new Uri("https://localhost:7286")); ``` -In a component, create an and request the component HTML (as `string`s), casting each to a for rendering: +In a component, inject and request the component HTML (as `string`s), casting each to a for rendering: + +> [!WARNING] +> Rendering raw HTML constructed from any untrusted source is a **security risk** and should **always** be avoided. ```razor @inject IHttpClientFactory ClientFactory From 896d6841f5c1f75df853de7aa45d3770e2d6ab3f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Jul 2026 16:31:00 +0000 Subject: [PATCH 5/6] Update monikerRange to >= aspnetcore-8.0 --- .../blazor/components/return-component-html-from-a-web-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/blazor/components/return-component-html-from-a-web-api.md b/aspnetcore/blazor/components/return-component-html-from-a-web-api.md index 60de496459f9..d2f658eaa6ed 100644 --- a/aspnetcore/blazor/components/return-component-html-from-a-web-api.md +++ b/aspnetcore/blazor/components/return-component-html-from-a-web-api.md @@ -3,7 +3,7 @@ title: Return Razor component HTML from a web API in ASP.NET Core Blazor ai-usage: ai-assisted author: guardrex description: Learn how to return HTML (as a string) from a web API (Minimal API) using the RazorComponentResult class. -monikerRange: '>= aspnetcore-6.0' +monikerRange: '>= aspnetcore-8.0' ms.author: wpickett ms.date: 07/31/2026 uid: blazor/components/return-component-html-from-a-web-api From 75fb4853c9ef188754ed02ff584782d30c392459 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 31 Jul 2026 17:36:38 +0000 Subject: [PATCH 6/6] Remove not-latest-version include and blank line (lines 13-14) --- .../blazor/components/return-component-html-from-a-web-api.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/aspnetcore/blazor/components/return-component-html-from-a-web-api.md b/aspnetcore/blazor/components/return-component-html-from-a-web-api.md index d2f658eaa6ed..b867a28e8a79 100644 --- a/aspnetcore/blazor/components/return-component-html-from-a-web-api.md +++ b/aspnetcore/blazor/components/return-component-html-from-a-web-api.md @@ -10,8 +10,6 @@ uid: blazor/components/return-component-html-from-a-web-api --- # Return Razor component HTML from a web API in ASP.NET Core Blazor -[!INCLUDE[](~/includes/not-latest-version.md)] - This article demonstrates how to return Razor component HTML (as a `string`) from a web API (Minimal API) using the class. Components returned via are rendered as static HTML strings. Features requiring an active SignalR connection for component interactivity can't execute in this context.