Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor. How can I get current url in the Startup class (method ConfigureServices)?

$
0
0

I am using AspNetCore.Identity.LiteDB. The database name depends on hostname. I am trying to get hostname using NavigationManager but it seems in the Startup.ConfigureServices it is not initialized.

public void ConfigureServices(IServiceCollection services)    {       // Server Side Blazor doesn't register HttpClient by default        if (!services.Any(x => x.ServiceType == typeof(HttpClient)))        {            // Setup HttpClient for server side in a client side compatible fashion            services.AddScoped<HttpClient>(s =>            {                NavigationManager uriHelper = s.GetRequiredService<NavigationManager>();                return new HttpClient                {                    BaseAddress = new Uri(uriHelper.BaseUri)                };            });        }        services.AddRazorPages();        services.AddServerSideBlazor();        services.AddTransient<ILiteDbContext>(s =>                {                    NavigationManager uriHelper = s.GetRequiredService<NavigationManager>();                    Uri currentUrl= uriHelper.ToAbsoluteUri(uriHelper.BaseUri);                    return new LiteDbContext(new LiteDatabase(this.GetUserDatabasePath(currentUrl)));                }            );        //services.AddTransient<ILiteDbContext, LiteDbContext>();        services.AddIdentity<ApplicationUser, AspNetCore.Identity.LiteDB.IdentityRole>(options =>        {            options.Password.RequireDigit = false;            options.Password.RequireUppercase = false;            options.Password.RequireLowercase = false;            options.Password.RequireNonAlphanumeric = false;            options.Password.RequiredLength = 6;        })        .AddUserStore<LiteDbUserStore<ApplicationUser>>()        .AddRoleStore<LiteDbRoleStore<AspNetCore.Identity.LiteDB.IdentityRole>>()        .AddDefaultTokenProviders();

I have the exception: InvalidOperationException: 'RemoteNavigationManager' has not been initialized.

enter image description here

As I understood NavigationManager is not initialized when Identity is being initialized. Is there a way to get the current URL without NavigationManager?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>