Repository: dotnet/aspnetcore
File: src/Components/Components/src/Routing/Router.cs (C#)

Code before:
            RenderNotFound();
        }
    }

    private void RenderNotFound()
    {
        _renderHandle.Render(builder =>
        {
            if (NotFoundPage != null)
            {
                if (_notFoundLayoutType is Type layoutType)
                {
                    // Directly instantiate the layout type, supplying the NotFoundPage as the Body
                    builder.OpenComponent(0, layoutType);
                    builder.AddAttribute(1, LayoutComponentBase.BodyPropertyName,
                        (RenderFragment)(childBuilder =>
                        {
                            childBuilder.OpenComponent(2, NotFoundPage);
                            childBuilder.CloseComponent();
                        }));
                    builder.CloseComponent();
                }
                else
                {
                    builder.OpenComponent(0, NotFoundPage);
                    builder.CloseComponent();
                }
            }
            else if (NotFound != null)
            {
                NotFound(builder);
            }
            else
            {
                DefaultNotFoundContent(builder);
            }
        });
    }

    async Task IHandleAfterRender.OnAfterRenderAsync()
    {
        if (!_navigationInterceptionEnabled)
        {
            _navigationInterceptionEnabled = true;
            await NavigationInterception.EnableNavigationInterceptionAsync();
        }

        if (_updateScrollPositionForHash)
        {
            _updateScrollPositionForHash = false;
            await ScrollToLocationHash.RefreshScrollPositionForHash(_locationAbsolute);

Review comment (suggestion):
Instead of manually searching for the layout and creating this whole structure, why don't we render a `RouteView` component and pass in the `_notfoundPageType` (and possibly empty parameters).

That allow us to reuse the logic and makes sure that if we ever need to change it, is in one place.

Code after:
        _renderHandle.Render(builder =>
        {
            if (NotFoundPage != null)
            {
                builder.OpenComponent<RouteView>(0);
                builder.AddAttribute(1, nameof(RouteView.RouteData),
                    new RouteData(NotFoundPage, _emptyParametersDictionary));
                builder.CloseComponent();
            }
            else if (NotFound != null)
            {
                NotFound(builder);
            }
            else
            {
                DefaultNotFoundContent(builder);
            }
        });
    }

    async Task IHandleAfterRender.OnAfterRenderAsync()
    {
        if (!_navigationInterceptionEnabled)
        {
            _navigationInterceptionEnabled = true;
            await NavigationInterception.EnableNavigationInterceptionAsync();
        }

        if (_updateScrollPositionForHash)
        {
            _updateScrollPositionForHash = false;
            await ScrollToLocationHash.RefreshScrollPositionForHash(_locationAbsolute);
        }
    }

    private static partial class Log
    {
        [LoggerMessage(1, LogLevel.Debug, $"Displaying {nameof(NotFound)} because path '{{Path}}' with base URI '{{BaseUri}}' does not match any component route", EventName = "DisplayingNotFound")]
        internal static partial void DisplayingNotFound(ILogger logger, string path, string baseUri);

        [LoggerMessage(2, LogLevel.Debug, "Navigating to component {ComponentType} in response to path '{Path}' with base URI '{BaseUri}'", EventName = "NavigatingToComponent")]
        internal static partial void NavigatingToComponent(ILogger logger, Type componentType, string path, string baseUri);

        [LoggerMessage(3, LogLevel.Debug, "Navigating to non-component URI '{ExternalUri}' in response to path '{Path}' with base URI '{BaseUri}'", EventName = "NavigatingToExternalUri")]
        internal static partial void NavigatingToExternalUri(ILogger logger, string externalUri, string path, string baseUri);

        [LoggerMessage(4, LogLevel.Debug, $"Displaying {nameof(NotFound)} on request", EventName = "DisplayingNotFoundOnRequest")]
        internal static partial void DisplayingNotFound(ILogger logger);
    }
}