File: Components\Pages\Home.razor.cs
Web Access
Project: src\src\Aspire.Dashboard\Aspire.Dashboard.csproj (Aspire.Dashboard)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using Aspire.Dashboard.Utils;
using Microsoft.AspNetCore.Components;
 
namespace Aspire.Dashboard.Components.Pages;
 
/// <summary>
/// The root URL. It redirects to the resources, with all resources selected, shown as the full resource list.
/// </summary>
/// <remarks>
/// The root URL used to host the resources page. Links created by older tools, such as
/// <c>/?resource=api</c> or <c>/?view=Graph</c>, are redirected to the equivalent view.
/// </remarks>
public sealed partial class Home : ComponentBase
{
    [SupplyParameterFromQuery(Name = "resource")]
    public string? LegacyResourceName { get; set; }
 
    [SupplyParameterFromQuery(Name = "view")]
    public string? LegacyViewKind { get; set; }
 
    [Inject]
    public required NavigationManager NavigationManager { get; init; }
 
    [Inject]
    public required IDashboardClient DashboardClient { get; init; }
 
    // The redirect runs after the first render, not in OnInitialized. When "/" is the first page the browser loads,
    // a navigation started while the circuit is still initializing is applied without its history entry state, so
    // the resources layout couldn't tell the home navigation apart from a plain link to /resources.
    protected override void OnAfterRender(bool firstRender)
    {
        if (!firstRender)
        {
            return;
        }
 
        var legacyUrl = TryGetLegacyRedirectUrl(LegacyResourceName, LegacyViewKind);
        var url = legacyUrl ?? (DashboardClient.IsEnabled ? DashboardUrls.ResourceListUrl() : DashboardUrls.StructuredLogsUrl());
        NavigationManager.NavigateTo(url, new NavigationOptions
        {
            ReplaceHistoryEntry = true,
            // The resources URL opens as the full resource list, the dashboard's home.
            HistoryEntryState = legacyUrl is null && DashboardClient.IsEnabled ? Aspire.Dashboard.Components.Layout.ResourcesLayout.ResourceListHomeHistoryEntryState : null
        });
    }
 
    internal static string? TryGetLegacyRedirectUrl(string? resourceName, string? viewKind)
    {
        if (string.Equals(viewKind, nameof(Resources.ResourceViewKind.Graph), StringComparison.OrdinalIgnoreCase))
        {
            return DashboardUrls.GraphUrl();
        }
 
        if (string.Equals(viewKind, nameof(Resources.ResourceViewKind.Parameters), StringComparison.OrdinalIgnoreCase))
        {
            return DashboardUrls.ResourceListUrl();
        }
 
        if (!string.IsNullOrEmpty(resourceName))
        {
            return DashboardUrls.ResourceOverviewUrl(resourceName);
        }
 
        return null;
    }
}