diff --git a/docs/fundamentals/custom-resource-urls.md b/docs/fundamentals/custom-resource-urls.md new file mode 100644 index 0000000000..7cb6d58d0c --- /dev/null +++ b/docs/fundamentals/custom-resource-urls.md @@ -0,0 +1,83 @@ +--- +title: Define custom resource URLs +description: Learn how to create custom URLs for .NET Aspire resources. +ms.date: 04/08/2025 +ms.topic: how-to +--- + +# Define custom resource URLs + +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. + +## Default endpoint behavior + +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. + +Likewise, you can explicitly expose endpoints using the 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://:` or `https://:`, depending on the protocol used. To omit the host port, use one of the following methods: + +- +- + +For more information, see [Endpoint extension methods](networking-overview.md#endpoint-extension-methods). + +## Supported resource types + +Currently, custom resource URLs are supported for the following resource types: + +- +- +- + +## Customize resource URLs + +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: + +:::code source="snippets/custom-urls/AspireApp.AppHost/Program.WithUrl.cs" id="withurl"::: + +> [!TIP] +> 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. + +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 and a display name as parameters. The resulting URL is available in the dashboard as shown in the following screenshot: + +:::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"::: + +### Customize endpoint URL + + + +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. + +To customize the URL for the first associated resource endpoint, use the `WithUrlForEndpoint` method. + +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. + +:::code source="snippets/custom-urls/AspireApp.AppHost/Program.WithUrlForEndpoint.cs" id="withurlforendpoint"::: + + + +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. + +When the resource is started, the URL is available in the dashboard as shown in the following screenshot: + +:::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"::: + +### Customize multiple resource URLs + + + +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: + +:::code source="snippets/custom-urls/AspireApp.AppHost/Program.WithUrls.cs" id="withurls"::: + +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: + +:::image type="content" source="dashboard/media/custom-urls/custom-url-ordered.png" alt-text=".NET Aspire dashboard custom ordered and named URLs."::: + +## URL customization lifecycle + +URL customization callbacks run during the application model lifecycle, specifically during the 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. + +## See also + +- [.NET Aspire dashboard overview](./overview.md) +- [.NET Aspire app host](../app-host.md) diff --git a/docs/fundamentals/dashboard/media/custom-urls/custom-url-admin-portal.png b/docs/fundamentals/dashboard/media/custom-urls/custom-url-admin-portal.png new file mode 100644 index 0000000000..82b095a149 Binary files /dev/null and b/docs/fundamentals/dashboard/media/custom-urls/custom-url-admin-portal.png differ diff --git a/docs/fundamentals/dashboard/media/custom-urls/custom-url-ordered.png b/docs/fundamentals/dashboard/media/custom-urls/custom-url-ordered.png new file mode 100644 index 0000000000..9408b51322 Binary files /dev/null and b/docs/fundamentals/dashboard/media/custom-urls/custom-url-ordered.png differ diff --git a/docs/fundamentals/dashboard/media/custom-urls/custom-url-scalar-https.png b/docs/fundamentals/dashboard/media/custom-urls/custom-url-scalar-https.png new file mode 100644 index 0000000000..52bc17aa15 Binary files /dev/null and b/docs/fundamentals/dashboard/media/custom-urls/custom-url-scalar-https.png differ diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.Api/AspireApp.Api.csproj b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/AspireApp.Api.csproj new file mode 100644 index 0000000000..60d98a3461 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/AspireApp.Api.csproj @@ -0,0 +1,14 @@ + + + + net9.0 + enable + enable + + + + + + + + diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.Api/AspireApp.Api.http b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/AspireApp.Api.http new file mode 100644 index 0000000000..0a7f89562e --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/AspireApp.Api.http @@ -0,0 +1,6 @@ +@AspireApp.Api_HostAddress = http://localhost:5020 + +GET {{AspireApp.Api_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.Api/AspireApp.Api.sln b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/AspireApp.Api.sln new file mode 100644 index 0000000000..0ad43994ec --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/AspireApp.Api.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.35906.104 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireApp.Api", "AspireApp.Api.csproj", "{BB5FD48A-DA3C-4E2C-B01B-FF8FB922F4F5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireApp.AppHost", "..\AspireApp.AppHost\AspireApp.AppHost.csproj", "{DC5D0ECC-8CBC-4436-B1DC-3D2681DD4B50}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BB5FD48A-DA3C-4E2C-B01B-FF8FB922F4F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BB5FD48A-DA3C-4E2C-B01B-FF8FB922F4F5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BB5FD48A-DA3C-4E2C-B01B-FF8FB922F4F5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BB5FD48A-DA3C-4E2C-B01B-FF8FB922F4F5}.Release|Any CPU.Build.0 = Release|Any CPU + {DC5D0ECC-8CBC-4436-B1DC-3D2681DD4B50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC5D0ECC-8CBC-4436-B1DC-3D2681DD4B50}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC5D0ECC-8CBC-4436-B1DC-3D2681DD4B50}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC5D0ECC-8CBC-4436-B1DC-3D2681DD4B50}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6D149486-7C78-4537-B183-314816378E9C} + EndGlobalSection +EndGlobal diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.Api/Program.cs b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/Program.cs new file mode 100644 index 0000000000..eeaf9e8127 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/Program.cs @@ -0,0 +1,120 @@ +using Scalar.AspNetCore; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi +builder.Services.AddOpenApi(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); + app.MapScalarApiReference(); +} + +app.UseHttpsRedirection(); + +var summaries = new[] +{ + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" +}; + +app.MapGet("/weatherforecast", () => +{ + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + return forecast; +}) +.WithName("GetWeatherForecast"); + +app.MapGet("/admin", () => +{ + return Results.Content(""" + + + + + Admin Portal Login + + + + +
+

Admin Portal

+ +
+ + + + + + + +
+
+ + + + """, "text/html"); +}); + +app.Run(); + +internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.Api/Properties/launchSettings.json b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/Properties/launchSettings.json new file mode 100644 index 0000000000..c95429b944 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5020", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7233;http://localhost:5020", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.Api/appsettings.Development.json b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/appsettings.Development.json new file mode 100644 index 0000000000..0c208ae918 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.Api/appsettings.json b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/appsettings.json new file mode 100644 index 0000000000..10f68b8c8b --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.Api/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/AspireApp.AppHost.csproj b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/AspireApp.AppHost.csproj new file mode 100644 index 0000000000..48cab46c70 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/AspireApp.AppHost.csproj @@ -0,0 +1,21 @@ + + + + + + Exe + net9.0 + enable + enable + 31d1f6cf-dfb5-4923-b92a-6c0e9e032f15 + + + + + + + + + + + diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.WithUrl.cs b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.WithUrl.cs new file mode 100644 index 0000000000..c059e8b7f0 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.WithUrl.cs @@ -0,0 +1,15 @@ +internal static partial class Program +{ + internal static void WithUrlExample(string[] args) + { + // + var builder = DistributedApplication.CreateBuilder(args); + + var api = builder.AddProject("api"); + + api.WithUrl($"{api.GetEndpoint("https")}/admin"), "Admin Portal"); + + builder.Build().Run(); + // + } +} diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.WithUrlForEndpoint.cs b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.WithUrlForEndpoint.cs new file mode 100644 index 0000000000..2653b40261 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.WithUrlForEndpoint.cs @@ -0,0 +1,18 @@ +internal static partial class Program +{ + internal static void WithUrlForEndpointExample(string[] args) + { + // + var builder = DistributedApplication.CreateBuilder(args); + + builder.AddProject("api") + .WithUrlForEndpoint("https", url => + { + url.DisplayText = "Scalar (HTTPS)"; + url.Url += "/scalar"; + }); + + builder.Build().Run(); + // + } +} diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.WithUrls.cs b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.WithUrls.cs new file mode 100644 index 0000000000..96ba21ae66 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.WithUrls.cs @@ -0,0 +1,29 @@ +internal static partial class Program +{ + internal static void WithUrlsExample(string[] args) + { + // + var builder = DistributedApplication.CreateBuilder(args); + + builder.AddProject("api") + .WithUrls(context => + { + foreach (var tuple in context.Urls + .Select(url => (Url: url, Uri: new Uri(url.Url))) + .OrderByDescending(_ => _.Uri.Scheme is "https") + .Select((pair, index) => (pair.Url, pair.Uri.Scheme, Index: index))) + { + var (url, scheme, index) = tuple; + + // Order HTTPS first. + var order = context.Urls.Count - 1 - index; + + url.DisplayText = $"{index + 1}. {scheme.ToUpper()}"; + url.DisplayOrder = order; + } + }); + + builder.Build().Run(); + // + } +} diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.cs b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.cs new file mode 100644 index 0000000000..f1ba2e6a61 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Program.cs @@ -0,0 +1,10 @@ +// Uncomment a single line to run the example + +// Admin Portal +WithUrlExample(args); + +// Ordered / Schemes +//WithUrlsExample(args); + +// Scalar (HTTPS) +//WithUrlForEndpointExample(args); diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Properties/launchSettings.json b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Properties/launchSettings.json new file mode 100644 index 0000000000..a3eefc2782 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17101;http://localhost:15035", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21040", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22015" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15035", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19142", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20239" + } + } + } +} diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/appsettings.Development.json b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/appsettings.Development.json new file mode 100644 index 0000000000..0c208ae918 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/appsettings.json b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/appsettings.json new file mode 100644 index 0000000000..31c092aa45 --- /dev/null +++ b/docs/fundamentals/snippets/custom-urls/AspireApp.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/docs/toc.yml b/docs/toc.yml index f232fe6767..abac1406f2 100644 --- a/docs/toc.yml +++ b/docs/toc.yml @@ -53,9 +53,14 @@ items: - name: Add Dockerfiles to the app model href: app-host/withdockerfile.md displayName: dockerfile,docker - - name: Networking overview - displayName: inner loop,local networking - href: fundamentals/networking-overview.md + - name: Networking + items: + - name: Overview + displayName: inner loop,local networking,networking + href: fundamentals/networking-overview.md + - name: Define custom resource URLs + displayName: withurl,withurlforendpoint,withurls + href: fundamentals/custom-resource-urls.md - name: Eventing in .NET Aspire href: app-host/eventing.md - name: Use external parameters