Aspire.Hosting.Yarp
13.1.0
Prefix Reserved
dotnet add package Aspire.Hosting.Yarp --version 13.1.0
NuGet\Install-Package Aspire.Hosting.Yarp -Version 13.1.0
<PackageReference Include="Aspire.Hosting.Yarp" Version="13.1.0" />
<PackageVersion Include="Aspire.Hosting.Yarp" Version="13.1.0" />
<PackageReference Include="Aspire.Hosting.Yarp" />
paket add Aspire.Hosting.Yarp --version 13.1.0
#r "nuget: Aspire.Hosting.Yarp, 13.1.0"
#:package Aspire.Hosting.Yarp@13.1.0
#addin nuget:?package=Aspire.Hosting.Yarp&version=13.1.0
#tool nuget:?package=Aspire.Hosting.Yarp&version=13.1.0
Aspire.Hosting.Yarp library
Provides extension methods and resource definitions for an Aspire AppHost to configure a YARP reverse proxy instance.
Getting started
Install the package
In your AppHost project, install the Aspire YARP Hosting library with NuGet:
dotnet add package Aspire.Hosting.Yarp
Usage examples
Programmatic configuration
The modern approach uses programmatic configuration with the WithConfiguration method:
var builder = DistributedApplication.CreateBuilder(args);
var backendService = builder.AddProject<Projects.Backend>("backend");
var frontendService = builder.AddProject<Projects.Frontend>("frontend");
var gateway = builder.AddYarp("gateway")
.WithConfiguration(yarp =>
{
// Add a catch-all route for the frontend
yarp.AddRoute(frontendService);
// Add a route with path prefix for the backend API
yarp.AddRoute("/api/{**catch-all}", backendService)
.WithTransformPathRemovePrefix("/api");
});
var app = builder.Build();
await app.RunAsync();
Configuration with external services
You can also route to external services:
var externalApi = builder.AddExternalService("external-api", "https://api.example.com");
var gateway = builder.AddYarp("gateway")
.WithConfiguration(yarp =>
{
yarp.AddRoute("/external/{**catch-all}", externalApi)
.WithTransformPathRemovePrefix("/external");
});
Static file serving
YARP can serve static files alongside proxied routes. There are two approaches:
Copy files locally
builder.AddYarp("static")
.WithStaticFiles("../static");
This will copy files into the container use container files in run mode, and use a bind mount in publish mode.
Copy files via Docker
You can also use a docker file to copy static assets into the yarp container: e.g.
builder.AddYarp("frontend")
.WithStaticFiles()
.WithDockerFile("../npmapp");
# Stage 1: Build React app
FROM node:20 AS builder
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
# Stage 2: Copy static files to YARP container
FROM mcr.microsoft.com/dotnet/nightly/yarp:2.3.0-preview.4 AS yarp
WORKDIR /app
COPY --from=builder /app/dist ./wwwroot
Configuration API
Route configuration
The IYarpConfigurationBuilder provides methods to configure routes and clusters:
// Add routes with different targets
yarp.AddRoute(resource); // Catch-all route
yarp.AddRoute("/path/{**catch-all}", resource); // Specific path route
yarp.AddRoute("/path/{**catch-all}", endpoint); // Route to specific endpoint
yarp.AddRoute("/path/{**catch-all}", externalService); // Route to external service
// Add clusters directly
var cluster = yarp.AddCluster(resource);
var route = yarp.AddRoute("/path/{**catch-all}", cluster);
Route matching options
Routes can be configured with various matching criteria:
yarp.AddRoute("/api/{**catch-all}", backendService)
.WithMatchMethods("GET", "POST") // HTTP methods
.WithMatchHeaders(new RouteHeader("Content-Type", "application/json")) // Headers
.WithMatchHosts("api.example.com") // Host header
.WithOrder(1); // Route priority
Transform extensions
YARP provides various transform extensions to modify requests and responses:
Path transforms
route.WithTransformPathSet("/new/path") // Set path
.WithTransformPathPrefix("/prefix") // Add prefix
.WithTransformPathRemovePrefix("/api") // Remove prefix
.WithTransformPathRouteValues("/users/{id}/posts"); // Use route values
Request header transforms
route.WithTransformRequestHeader("X-Forwarded-For", "value") // Add/set header
.WithTransformRequestHeaderRouteValue("X-User-Id", "id") // From route value
.WithTransformUseOriginalHostHeader(true) // Preserve host
.WithTransformCopyRequestHeaders(false); // Copy headers
Response transforms
route.WithTransformResponseHeader("X-Powered-By", "Aspire") // Add response header
.WithTransformResponseHeaderRemove("Server") // Remove header
.WithTransformCopyResponseHeaders(true); // Copy headers
Query parameter transforms
route.WithTransformQueryValue("version", "1.0") // Add query param
.WithTransformQueryRouteValue("userId", "id") // From route value
.WithTransformQueryRemoveKey("debug"); // Remove query param
Advanced configuration
Multiple routes to the same service
builder.AddYarp("gateway")
.WithConfiguration(yarp =>
{
// Different routes to the same backend
yarp.AddRoute("/api/v1/{**catch-all}", backendService)
.WithTransformPathRemovePrefix("/api/v1");
yarp.AddRoute("/api/v2/{**catch-all}", backendService)
.WithTransformPathRemovePrefix("/api/v2")
.WithTransformPathPrefix("/v2");
});
Additional documentation
Feedback & contributing
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Aspire.Hosting (>= 13.1.0)
- AspNetCore.HealthChecks.Uris (>= 9.0.0)
- Google.Protobuf (>= 3.33.0)
- Grpc.AspNetCore (>= 2.71.0)
- Grpc.Net.ClientFactory (>= 2.71.0)
- Grpc.Tools (>= 2.72.0)
- Humanizer.Core (>= 2.14.1)
- JsonPatch.Net (>= 3.3.0)
- KubernetesClient (>= 18.0.5)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.22)
- Microsoft.Extensions.FileSystemGlobbing (>= 10.0.1)
- Microsoft.Extensions.Hosting (>= 8.0.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Primitives (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.4)
- Polly.Core (>= 8.6.4)
- Semver (>= 3.0.0)
- StreamJsonRpc (>= 2.22.23)
- System.IO.Hashing (>= 9.0.10)
- Yarp.ReverseProxy (>= 2.3.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Aspire.Hosting.Yarp:
| Package | Downloads |
|---|---|
|
NapalmCodes.Aspire.Hosting.Krakend
Aspire hosting component for the high performance KrakenD (https://www.krakend.io/) API Gateway. |
GitHub repositories (3)
Showing the top 3 popular GitHub repositories that depend on Aspire.Hosting.Yarp:
| Repository | Stars |
|---|---|
|
dotnet/eShop
A reference .NET application implementing an eCommerce site
|
|
|
foxminchan/BookWorm
The practical implementation of Aspire using Microservices, AI-Agents
|
|
|
davidfowl/aspire-ssh-deploy
An extension of the docker compose integration for deploying to a docker host via SSH
|
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 13.1.0 | 10,968 | 12/17/2025 | |
| 13.0.2 | 5,072 | 12/4/2025 | |
| 13.0.1 | 5,003 | 11/26/2025 | |
| 13.0.0 | 10,240 | 11/11/2025 | |
| 9.5.2-preview.1.25522.3 | 5,198 | 10/23/2025 | |
| 9.5.1-preview.1.25502.11 | 5,508 | 10/3/2025 | |
| 9.5.0-preview.1.25474.7 | 6,821 | 9/25/2025 | |
| 9.4.2-preview.1.25428.12 | 6,234 | 9/2/2025 | |
| 9.4.1-preview.1.25408.4 | 2,816 | 8/12/2025 | |
| 9.4.0-preview.1.25378.8 | 9,010 | 7/29/2025 | |
| 9.3.1-preview.1.25305.6 | 4,682 | 6/10/2025 | |
| 9.3.0-preview.1.25265.20 | 2,720 | 5/19/2025 |