Skip to content

Support NotFound content rendering for a custom Router when response has started #62367

Open
@ilonatommy

Description

@ilonatommy

Fix the last row in scenario matrix: #62153.

Internally, we are able to inform NavigationManager about the route to NotFoundPage:

    // CustomRouter.cs

    [Inject] private NavigationManager NavigationManager { get; set; }
    public Type NotFoundPage { get; set; } = default!;

    public async Task SetParametersAsync(ParameterView parameters)
    {
        if (NotFoundPage != null)
        {
            if (!typeof(IComponent).IsAssignableFrom(NotFoundPage))
            {
                throw new InvalidOperationException($"The type {NotFoundPage.FullName} " +
                    $"does not implement {typeof(IComponent).FullName}.");
            }
            var routeAttributes = NotFoundPage.GetCustomAttributes(typeof(RouteAttribute), inherit: true);
            if (routeAttributes.Length == 0)
            {
                throw new InvalidOperationException($"The type {NotFoundPage.FullName} " +
                    $"does not have a {typeof(RouteAttribute).FullName} applied to it.");
            }

            var routeAttribute = (RouteAttribute)routeAttributes[0];
            if (routeAttribute.Template != null)
            {
                NavigationManager.NotFoundPageRoute = routeAttribute.Template;
            }
        }
    }

Custom routers don't have access to internal NavigationManager.NotFoundPageRoute. Change it, possibly to subscription to NotFound event in Invoker and passing the information though it.

Metadata

Metadata

Assignees

Labels

area-blazorIncludes: Blazor, Razor Components

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions