gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:06:48 26 4
gpt4 key购买 nike

我正在使用 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”尚未初始化。

enter image description here

据我了解,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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com