c# - 开拓者。如何在 Startup 类(方法 ConfigureServices)中获取当前

我正在使用 AspNetCore.Identity.LiteDB。数据库名称取决于主机名。我正在尝试使用 NavigationManager 获取主机名,但它似乎在 Startup.ConfigureServices 中未初始化。

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();

我有一个异常(exception): InvalidOperationException:“RemoteNavigationManager”尚未初始化。

据我了解,NavigationManager 在初始化 Identity 时未初始化。有没有办法在没有 NavigationManager 的情况下获取当前 URL?

最佳答案

这是 Blazor 服务器端还是 Blazor 客户端?
如果是客户端,您可以使用 IWebAssemblyHostEnvironment 接口(interface)获取应用程序的 BaseAddress。 您可以在 CSB Program.cs

中执行类似的操作
private static void ConfigureServices(IServiceCollection services, IWebAssemblyHostEnvironment hostEnvironment)
{
    var baseAddress = hostEnvironment.BaseAddress;
    // Do your stuff
}

关于c# - 开拓者。如何在 Startup 类(方法 ConfigureServices)中获取当前 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62377889/

相关文章:

reactjs - Jest 测试在 react 加载中失败

html - 将样式添加到 Bootstrap 表单

php - 如何在 laravel 中检测 JFIF 图像类型

python - ImportError : libX11. so.6: 无法打开共享对象文件: 没

javascript - 无法在 'pipeTo' : Illegal invocation 上执行

javascript - 单击 jpg 图像时尝试使用 EXIF.js 返回 GPS 坐标

vue.js - 在 Vue 中安装不想给 $route.name

tensorflow - TensorFlow 2 和 Keras 中不同的正向和反向传播

ios - iOS 13 的 iOS 多任务 View 中的错误应用程序图标

python - 获取实例的类名