[英]Blazor. How can I get current url in the Startup class (method ConfigureServices)?
I am using AspNetCore.Identity.LiteDB.我正在使用 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.
我正在尝试使用 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();
I have the exception: InvalidOperationException: 'RemoteNavigationManager' has not been initialized.我有一个例外: InvalidOperationException: 'RemoteNavigationManager' 尚未初始化。
As I understood NavigationManager is not initialized when Identity is being initialized.据我了解,在初始化 Identity 时 NavigationManager 未初始化。 Is there a way to get the current URL without NavigationManager?
有没有办法在没有 NavigationManager 的情况下获取当前的 URL?
Is this Blazor server-side or blazor-client-side?这是 Blazor 服务器端还是 blazor 客户端?
If client-side you can use the IWebAssemblyHostEnvironment
interface to get the BaseAddress
of the app.如果是客户端,您可以使用
IWebAssemblyHostEnvironment
接口来获取应用程序的BaseAddress
。 You can do something like this in your CSB Program.cs
你可以在你的 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.我托管了 Blazor WASM,我的服务器需要主机 uri 才能将其作为回调参数提供给第 3 方。
In case you use .razor
file on the server you can use:如果您在服务器上使用
.razor
文件,您可以使用:
NavigationManager.BaseUri
But in my case, in API call I have used Request information to get needed uri:但就我而言,在 API 调用中,我使用了请求信息来获取所需的 uri:
[AllowAnonymous]
[HttpGet("EndpointName")]
public ActionResult EndpointName()
{
var serverUri = $"{Request.Scheme}://{Request.Host.Value}";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.