Search code examples
c#asp.net-coreblazorasp.net-identity

Where does the CascadingParameter HttpContext come from in a Blazor server side app with identity?


Im currently learning blazor identity and am analyzing the built in identity files. Im not one to just use something without knowing more about what its doing so i was just curious if anyone could explain to me where the following line of code cascades from?

[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;

Solution

  • You could check the source codes of AddRazorComponents method

    public static class RazorComponentsServiceCollectionExtensions
    {
        /// <summary>
        /// Registers services required for server-side rendering of Razor Components.
        /// </summary>
        /// <param name="services">The service collection.</param>
        /// <param name="configure">An <see cref="Action{RazorComponentOptions}"/> to configure the provided <see cref="RazorComponentsServiceOptions"/>.</param>
        /// <returns>An <see cref="IRazorComponentsBuilder"/> that can be used to further configure the Razor component services.</returns>
        [RequiresUnreferencedCode("Razor Components does not currently support trimming or native AOT.", Url = "https://aka.ms/aspnet/nativeaot")]
        public static IRazorComponentsBuilder AddRazorComponents(this IServiceCollection services, Action<RazorComponentsServiceOptions>? configure = null)
        {
            ArgumentNullException.ThrowIfNull(services);
    
            // Dependencies
            services.AddLogging();
            services.AddAntiforgery();
    
            services.TryAddSingleton<RazorComponentsMarkerService>();
    
            // Endpoints
            services.TryAddSingleton<RazorComponentEndpointDataSourceFactory>();
            services.TryAddSingleton<RazorComponentEndpointFactory>();
            if (MetadataUpdater.IsSupported)
            {
                services.TryAddSingleton<HotReloadService>();
            }
            services.TryAddScoped<IRazorComponentEndpointInvoker, RazorComponentEndpointInvoker>();
    
            // Common services required for components server side rendering
            services.TryAddSingleton<ServerComponentSerializer>(services => new ServerComponentSerializer(services.GetRequiredService<IDataProtectionProvider>()));
            services.TryAddSingleton<WebAssemblyComponentSerializer>();
            services.TryAddScoped<EndpointHtmlRenderer>();
            services.TryAddScoped<IComponentPrerenderer>(services => services.GetRequiredService<EndpointHtmlRenderer>());
            services.TryAddScoped<NavigationManager, HttpNavigationManager>();
            services.TryAddScoped<IJSRuntime, UnsupportedJavaScriptRuntime>();
            services.TryAddScoped<INavigationInterception, UnsupportedNavigationInterception>();
            services.TryAddScoped<IScrollToLocationHash, UnsupportedScrollToLocationHash>();
            services.TryAddScoped<ComponentStatePersistenceManager>();
            services.TryAddScoped<PersistentComponentState>(sp => sp.GetRequiredService<ComponentStatePersistenceManager>().State);
            services.TryAddScoped<IErrorBoundaryLogger, PrerenderingErrorBoundaryLogger>();
            services.TryAddEnumerable(
                ServiceDescriptor.Singleton<IPostConfigureOptions<RazorComponentsServiceOptions>, DefaultRazorComponentsServiceOptionsConfiguration>());
            services.TryAddScoped<EndpointRoutingStateProvider>();
            services.TryAddScoped<IRoutingStateProvider>(sp => sp.GetRequiredService<EndpointRoutingStateProvider>());
            services.AddSupplyValueFromQueryProvider();
            services.TryAddCascadingValue(sp => sp.GetRequiredService<EndpointHtmlRenderer>().HttpContext);
    
            // Form handling
            services.AddSupplyValueFromFormProvider();
            services.TryAddScoped<AntiforgeryStateProvider, EndpointAntiforgeryStateProvider>();
            services.TryAddScoped<HttpContextFormDataProvider>();
            services.TryAddScoped<IFormValueMapper, HttpContextFormValueMapper>();
    
            if (configure != null)
            {
                services.Configure(configure);
            }
    
            return new DefaultRazorComponentsBuilder(services);
        }
    
        private sealed class DefaultRazorComponentsBuilder(IServiceCollection services) : IRazorComponentsBuilder
        {
            public IServiceCollection Services { get; } = services;
        }
    }
    

    Here specify a cascading value

    services.TryAddCascadingValue(sp => sp.GetRequiredService<EndpointHtmlRenderer>().HttpContext);
    

    Here's the document related with cascading parameter in .net 8