Create simpler way to show Not Found 404 UI with Blazor · Issue #45654 · dotnet/aspnetcore (original) (raw)

Is there an existing issue for this?

Currently, we support the lack of a page in blazor, unfortunately, if we want to place a 404 header for SEO and, for example, google bot, it creates a problem.
It's worth handling this exception.
There is currently a workaround that doesn't look very good to me.

App.razor

    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(Pages._404)">
            404
        </LayoutView>
    </NotFound>

404 layout:

@layout Shared.MainLayout @inherits LayoutComponentBase @using Microsoft.AspNetCore.Http

@Body

@code { [Inject] IHttpContextAccessor httpContextAccessor { get; set; } = null!;

[Inject] PersistentComponentState _ApplicationState { get; set; } = null!;

PersistingComponentStateSubscription persistingSubscription;

bool model = true;

protected override void OnInitialized()
{
    persistingSubscription = _ApplicationState.RegisterOnPersisting(Task () => { _ApplicationState.PersistAsJson($"404", model); return Task.CompletedTask; });
    if (!_ApplicationState.TryTakeFromJson<bool>($"404", out var dto))
    {
        if (httpContextAccessor.HttpContext is not null)
            httpContextAccessor.HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;
    }
}

}

Also I read in the documentation that we shouldn't use httpcontext in blazor subpages.

Describe the solution you'd like

I think Not Found blazor server side should automatically handle and ship. I've searched the internet and the documentation and found nothing useful.

Additional context

No response