Repository: dotnet/aspnetcore
File: src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs (C#)

Code before:
        {
            antiforgery.SetRequestContext(httpContext);
        }

        // It's important that this is initialized since a component might try to restore state during prerendering
        // (which will obviously not work, but should not fail)
        var componentApplicationLifetime = httpContext.RequestServices.GetRequiredService<ComponentStatePersistenceManager>();
        componentApplicationLifetime.SetPlatformRenderMode(RenderMode.InteractiveAuto);
        await componentApplicationLifetime.RestoreStateAsync(new PrerenderComponentApplicationStore());

        if (componentType != null)
        {
            // Saving RouteData to avoid routing twice in Router component
            var routingStateProvider = httpContext.RequestServices.GetRequiredService<EndpointRoutingStateProvider>();
            routingStateProvider.RouteData = new RouteData(componentType, httpContext.GetRouteData().Values);
            if (httpContext.GetEndpoint() is RouteEndpoint routeEndpoint)
            {
                routingStateProvider.RouteData.Template = routeEndpoint.RoutePattern.RawText;
            }
        }
    }

    private void OnNavigateTo(object? sender, NavigationEventArgs args)
    {
        _httpContext.Response.Redirect(args.Uri);
    }

    private static void InitializeResourceCollection(HttpContext httpContext)
    {

        var endpoint = httpContext.GetEndpoint();
        var resourceCollection = GetResourceCollection(httpContext);
        var resourceCollectionUrl = resourceCollection != null && endpoint != null ?
            endpoint.Metadata.GetMetadata<ResourceCollectionUrlMetadata>() :
            null;

        var resourceCollectionProvider = resourceCollectionUrl != null ? httpContext.RequestServices.GetService<ResourceCollectionProvider>() : null;
        if (resourceCollectionUrl != null && resourceCollectionProvider != null)
        {
            resourceCollectionProvider.SetResourceCollectionUrl(resourceCollectionUrl.Url);
            resourceCollectionProvider.SetResourceCollection(resourceCollection ?? ResourceAssetCollection.Empty);
        }
    }

    protected override ComponentState CreateComponentState(int componentId, IComponent component, ComponentState? parentComponentState)
        => new EndpointComponentState(this, componentId, component, parentComponentState);

    /// <inheritdoc/>
    protected override ResourceAssetCollection Assets =>
        _resourceCollection ??= GetResourceCollection(_httpContext) ?? base.Assets;


Review comment (suggestion):
I'm not sure this is enough. We need to do something like what we do in the NotFound case, where we stop further renders, as they are not going to be used.

Also, in the case the response has started (streaming rendering), we can't start a redirect this way

Code after:
        // It's important that this is initialized since a component might try to restore state during prerendering
        // (which will obviously not work, but should not fail)
        var componentApplicationLifetime = httpContext.RequestServices.GetRequiredService<ComponentStatePersistenceManager>();
        componentApplicationLifetime.SetPlatformRenderMode(RenderMode.InteractiveAuto);
        await componentApplicationLifetime.RestoreStateAsync(new PrerenderComponentApplicationStore());

        if (componentType != null)
        {
            // Saving RouteData to avoid routing twice in Router component
            var routingStateProvider = httpContext.RequestServices.GetRequiredService<EndpointRoutingStateProvider>();
            routingStateProvider.RouteData = new RouteData(componentType, httpContext.GetRouteData().Values);
            if (httpContext.GetEndpoint() is RouteEndpoint routeEndpoint)
            {
                routingStateProvider.RouteData.Template = routeEndpoint.RoutePattern.RawText;
            }
        }
    }

    private static void InitializeResourceCollection(HttpContext httpContext)
    {

        var endpoint = httpContext.GetEndpoint();
        var resourceCollection = GetResourceCollection(httpContext);
        var resourceCollectionUrl = resourceCollection != null && endpoint != null ?
            endpoint.Metadata.GetMetadata<ResourceCollectionUrlMetadata>() :
            null;

        var resourceCollectionProvider = resourceCollectionUrl != null ? httpContext.RequestServices.GetService<ResourceCollectionProvider>() : null;
        if (resourceCollectionUrl != null && resourceCollectionProvider != null)
        {
            resourceCollectionProvider.SetResourceCollectionUrl(resourceCollectionUrl.Url);
            resourceCollectionProvider.SetResourceCollection(resourceCollection ?? ResourceAssetCollection.Empty);
        }
    }

    protected override ComponentState CreateComponentState(int componentId, IComponent component, ComponentState? parentComponentState)
        => new EndpointComponentState(this, componentId, component, parentComponentState);

    /// <inheritdoc/>
    protected override ResourceAssetCollection Assets =>
        _resourceCollection ??= GetResourceCollection(_httpContext) ?? base.Assets;

    private static ResourceAssetCollection? GetResourceCollection(HttpContext httpContext) => httpContext.GetEndpoint()?.Metadata.GetMetadata<ResourceAssetCollection>();

    protected override void AddPendingTask(ComponentState? componentState, Task task)
    {
        var streamRendering = componentState is null
            ? false
            : ((EndpointComponentState)componentState).StreamRendering;

        if (!streamRendering)