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

Viewed 3194

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?

2 Answers

Is this Blazor server-side or blazor-client-side?
If client-side you can use the IWebAssemblyHostEnvironment interface to get the BaseAddress of the app. You can do something like this in your CSB Program.cs

private static void ConfigureServices(IServiceCollection services, IWebAssemblyHostEnvironment hostEnvironment)
{
    var baseAddress = hostEnvironment.BaseAddress;
    // Do your stuff
}

I have Blazor WASM hosted and my server needed the host uri in order to provide it to 3rd party as a callback parameter.

In case you use .razor file on the server you can use:

NavigationManager.BaseUri

But in my case, in API call I have used Request information to get needed uri:

[AllowAnonymous]
[HttpGet("EndpointName")]
public ActionResult EndpointName()
{
    var serverUri = $"{Request.Scheme}://{Request.Host.Value}";
}
Related