aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

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

Open ilonatommy opened this issue 5 months ago • 0 comments

Fix the last row in scenario matrix: https://github.com/dotnet/aspnetcore/issues/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.

ilonatommy avatar Jun 17 '25 09:06 ilonatommy