Skip to content

Commit 3cbbfb3

Browse files
IEvangelistgewarrenDamianEdwards
authored
Add content on customizing resource URLs (#2940)
* Fixes #2936 * Correct TOC * Minor tweaks * Apply suggestions from code review Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Damian Edwards <damian@damianedwards.com> * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review * Apply suggestions from code review --------- Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Co-authored-by: Damian Edwards <damian@damianedwards.com>
1 parent 9278dda commit 3cbbfb3

20 files changed

+440
-3
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: Define custom resource URLs
3+
description: Learn how to create custom URLs for .NET Aspire resources.
4+
ms.date: 04/08/2025
5+
ms.topic: how-to
6+
---
7+
8+
# Define custom resource URLs
9+
10+
In .NET Aspire, resources that expose endpoints only configure host and port, which aren't known until run time. If you need to access a specific path on one of these endpoints—especially from the [dashboard](dashboard/overview.md)—you can define custom resource URLs. You can also add custom URLs that aren't tied to any endpoint. All custom URLs are only available in "run" mode, since they're meant for dashboard use. This article demonstrates how to define custom URLs.
11+
12+
## Default endpoint behavior
13+
14+
By default, .NET Aspire project resources rely on existing configurations such as Kestrel or launch profiles to determine the host and port of a resource for a configured endpoint.
15+
16+
Likewise, you can explicitly expose endpoints using the <xref:Aspire.Hosting.ResourceBuilderExtensions.WithEndpoint*> API. This API allows you to specify the host and port for a resource, which is then used to create the default URL for that resource. The default URL is typically in the format `http://<host>:<port>` or `https://<host>:<port>`, depending on the protocol used. To omit the host port, use one of the following methods:
17+
18+
- <xref:Aspire.Hosting.ResourceBuilderExtensions.WithHttpEndpoint*>
19+
- <xref:Aspire.Hosting.ResourceBuilderExtensions.WithHttpsEndpoint*>
20+
21+
For more information, see [Endpoint extension methods](networking-overview.md#endpoint-extension-methods).
22+
23+
## Supported resource types
24+
25+
Currently, custom resource URLs are supported for the following resource types:
26+
27+
- <xref:Aspire.Hosting.ApplicationModel.ContainerResource>
28+
- <xref:Aspire.Hosting.ApplicationModel.ExecutableResource>
29+
- <xref:Aspire.Hosting.ApplicationModel.ProjectResource>
30+
31+
## Customize resource URLs
32+
33+
Use the appropriate `WithUrl` overload, `WithUrls`, or `WithUrlForEndpoint` APIs on any supported resource builder to define custom URLs for a resource. The following example demonstrates how to set a custom URL for a project resource:
34+
35+
:::code source="snippets/custom-urls/AspireApp.AppHost/Program.WithUrl.cs" id="withurl":::
36+
37+
> [!TIP]
38+
> There's an overload that accepts a `string` allowing you to pass any URL. This is useful for defining custom URLs that aren't directly related to the resource's endpoint.
39+
40+
The preceding code assigns a project reference to the `api` variable, which is then used to create a custom URL for the `Admin Portal` route. The `WithUrl` method takes a <xref:Aspire.Hosting.ApplicationModel.ReferenceExpression> and a display name as parameters. The resulting URL is available in the dashboard as shown in the following screenshot:
41+
42+
:::image type="content" source="dashboard/media/custom-urls/custom-url-admin-portal.png" alt-text=".NET Aspire dashboard custom Admin Portal URL." lightbox="dashboard/media/custom-urls/custom-url-admin-portal.png":::
43+
44+
### Customize endpoint URL
45+
46+
<!-- TODO: Add xref to WithUrlForEndpoint when available -->
47+
48+
Both [Scalar](https://scalar.com/) and [Swagger](https://swagger.io/tools/swagger-ui/) are common API services that enhance the usability of endpoints. These services are accessed via URLs tied to declared endpoints.
49+
50+
To customize the URL for the first associated resource endpoint, use the `WithUrlForEndpoint` method.
51+
52+
If you want to add a separate URL (even for the same endpoint) you need to call the `WithUrl` overload that takes a `ReferenceExpression` or interpolated string, or call `WithUrls` and add the URL to the `Urls` list on the context.
53+
54+
:::code source="snippets/custom-urls/AspireApp.AppHost/Program.WithUrlForEndpoint.cs" id="withurlforendpoint":::
55+
56+
<!-- TODO: Add xref to ResourceUrlAnnotation when available -->
57+
58+
The preceding example assumes that the `api` project resource has an `https` endpoint configured. The `WithUrlForEndpoint` method updates the `ResourceUrlAnnotation` associated with the endpoint. In this case, it assigns the display text to `Scalar (HTTPS)` and appends the `/scalar` path to the URL.
59+
60+
When the resource is started, the URL is available in the dashboard as shown in the following screenshot:
61+
62+
:::image type="content" source="dashboard/media/custom-urls/custom-url-scalar-https.png" alt-text=".NET Aspire dashboard with custom Scalar URL." lightbox="dashboard/media/custom-urls/custom-url-scalar-https.png":::
63+
64+
### Customize multiple resource URLs
65+
66+
<!-- TODO: Add xref to WithUrls when available -->
67+
68+
To customize multiple URLs for a resource, use the `WithUrls` method. This method allows you to specify multiple URLs for a resource, each with its own display text. The following example demonstrates how to set multiple URLs for a project resource:
69+
70+
:::code source="snippets/custom-urls/AspireApp.AppHost/Program.WithUrls.cs" id="withurls":::
71+
72+
The preceding code iterates through the URLs defined for the `api` project resource and assigns a display text and order to each URL. The resulting URLs are available in the dashboard as shown in the following screenshot:
73+
74+
:::image type="content" source="dashboard/media/custom-urls/custom-url-ordered.png" alt-text=".NET Aspire dashboard custom ordered and named URLs.":::
75+
76+
## URL customization lifecycle
77+
78+
URL customization callbacks run during the application model lifecycle, specifically during the <xref:Aspire.Hosting.ApplicationModel.BeforeResourceStartedEvent> event processing. URLs associated with endpoints become active and appear on the dashboard once the endpoint itself becomes active. URLs not associated with endpoints become active only when the resource enters the "Running" state. This ensures that all custom URLs are accurately represented and available when the application resources are fully operational.
79+
80+
## See also
81+
82+
- [.NET Aspire dashboard overview](./overview.md)
83+
- [.NET Aspire app host](../app-host.md)
38.2 KB
Loading
34.9 KB
Loading
36.2 KB
Loading
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
11+
<PackageReference Include="Scalar.AspNetCore" Version="2.1.7" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@AspireApp.Api_HostAddress = http://localhost:5020
2+
3+
GET {{AspireApp.Api_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.14.35906.104
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireApp.Api", "AspireApp.Api.csproj", "{BB5FD48A-DA3C-4E2C-B01B-FF8FB922F4F5}"
6+
EndProject
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireApp.AppHost", "..\AspireApp.AppHost\AspireApp.AppHost.csproj", "{DC5D0ECC-8CBC-4436-B1DC-3D2681DD4B50}"
8+
EndProject
9+
Global
10+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
11+
Debug|Any CPU = Debug|Any CPU
12+
Release|Any CPU = Release|Any CPU
13+
EndGlobalSection
14+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
15+
{BB5FD48A-DA3C-4E2C-B01B-FF8FB922F4F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
16+
{BB5FD48A-DA3C-4E2C-B01B-FF8FB922F4F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
17+
{BB5FD48A-DA3C-4E2C-B01B-FF8FB922F4F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{BB5FD48A-DA3C-4E2C-B01B-FF8FB922F4F5}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{DC5D0ECC-8CBC-4436-B1DC-3D2681DD4B50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{DC5D0ECC-8CBC-4436-B1DC-3D2681DD4B50}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{DC5D0ECC-8CBC-4436-B1DC-3D2681DD4B50}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{DC5D0ECC-8CBC-4436-B1DC-3D2681DD4B50}.Release|Any CPU.Build.0 = Release|Any CPU
23+
EndGlobalSection
24+
GlobalSection(SolutionProperties) = preSolution
25+
HideSolutionNode = FALSE
26+
EndGlobalSection
27+
GlobalSection(ExtensibilityGlobals) = postSolution
28+
SolutionGuid = {6D149486-7C78-4537-B183-314816378E9C}
29+
EndGlobalSection
30+
EndGlobal
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using Scalar.AspNetCore;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
// Add services to the container.
6+
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
7+
builder.Services.AddOpenApi();
8+
9+
var app = builder.Build();
10+
11+
// Configure the HTTP request pipeline.
12+
if (app.Environment.IsDevelopment())
13+
{
14+
app.MapOpenApi();
15+
app.MapScalarApiReference();
16+
}
17+
18+
app.UseHttpsRedirection();
19+
20+
var summaries = new[]
21+
{
22+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
23+
};
24+
25+
app.MapGet("/weatherforecast", () =>
26+
{
27+
var forecast = Enumerable.Range(1, 5).Select(index =>
28+
new WeatherForecast
29+
(
30+
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
31+
Random.Shared.Next(-20, 55),
32+
summaries[Random.Shared.Next(summaries.Length)]
33+
))
34+
.ToArray();
35+
return forecast;
36+
})
37+
.WithName("GetWeatherForecast");
38+
39+
app.MapGet("/admin", () =>
40+
{
41+
return Results.Content("""
42+
<!DOCTYPE html>
43+
<html lang="en">
44+
<head>
45+
<meta charset="UTF-8">
46+
<title>Admin Portal Login</title>
47+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
48+
</head>
49+
<body style="
50+
margin: 0;
51+
padding: 0;
52+
box-sizing: border-box;
53+
font-family: 'Segoe UI', sans-serif;
54+
background: linear-gradient(to right, #667eea, #764ba2);
55+
height: 100vh;
56+
display: flex;
57+
align-items: center;
58+
justify-content: center;
59+
">
60+
61+
<div style="
62+
background-color: white;
63+
padding: 2rem;
64+
border-radius: 12px;
65+
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
66+
width: 100%;
67+
max-width: 400px;
68+
">
69+
<h2 style="
70+
margin-bottom: 1.5rem;
71+
text-align: center;
72+
color: #333;
73+
">Admin Portal</h2>
74+
75+
<form>
76+
<label for="email" style="display: block; margin-bottom: 0.5rem; color: #555;">Email</label>
77+
<input type="email" id="email" name="email" placeholder="admin@example.com" style="
78+
width: 100%;
79+
padding: 0.75rem;
80+
margin-bottom: 1rem;
81+
border: 1px solid #ccc;
82+
border-radius: 8px;
83+
font-size: 1rem;
84+
" required>
85+
86+
<label for="password" style="display: block; margin-bottom: 0.5rem; color: #555;">Password</label>
87+
<input type="password" id="password" name="password" placeholder="••••••••" style="
88+
width: 100%;
89+
padding: 0.75rem;
90+
margin-bottom: 1.5rem;
91+
border: 1px solid #ccc;
92+
border-radius: 8px;
93+
font-size: 1rem;
94+
" required>
95+
96+
<button type="submit" style="
97+
width: 100%;
98+
padding: 0.75rem;
99+
background-color: #667eea;
100+
color: white;
101+
border: none;
102+
border-radius: 8px;
103+
font-size: 1rem;
104+
cursor: pointer;
105+
transition: background-color 0.3s ease;
106+
">Login</button>
107+
</form>
108+
</div>
109+
110+
</body>
111+
</html>
112+
""", "text/html");
113+
});
114+
115+
app.Run();
116+
117+
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
118+
{
119+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
120+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": false,
8+
"applicationUrl": "http://localhost:5020",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": false,
17+
"applicationUrl": "https://localhost:7233;http://localhost:5020",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)