Skip to content

Create simpler way to show Not Found 404 UI with Blazor #45654

@Alerinos

Description

@Alerinos

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

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

Metadata

Metadata

Assignees

Labels

Pillar: Complete Blazor WebPriority:1Work that is critical for the release, but we could probably ship withoutarea-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-routingFeatures related to routing in Blazor applicationstriaged

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions