|
| 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) |
0 commit comments