简体   繁体   English

Blazor。 如何在启动 class(方法 ConfigureServices)中获取当前的 url?

[英]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.

相关问题 需要在 Startup.cs 的 ConfigureServices 方法中获取站点 URL - Need to get site URL in ConfigureServices method of Startup.cs 如何以及谁在 .net 内核中调用启动 class 的 ConfigureServices 和 Configure 方法 - How and Who calling the ConfigureServices and Configure method of startup class in .net core 如何将ConfigureServices方法(启动)拆分为多个文件 - How to Split ConfigureServices method (of Startup) into multiple files 如何在 ConfigureServices 方法中获取 IOptions? - How to get IOptions in ConfigureServices method? 我可以在启动(.NET Core)的 configureservices 方法中创建服务实例并将列表添加到属性吗? - Can I make an instance of a service in configureservices method in startup (.NET Core) and add a list to a property? 如何获取我在Asp.Net核心的Startup.ConfigureServices中添加的所有授权策略? - How to get all Authorize Policies that I added in the Startup.ConfigureServices in Asp.Net core? OWIN Startup方法如何获取网站的基本URL? - How can an OWIN Startup method get the base URL of the web site? 在 Blazor 组件中获取当前 URL - Get current URL in a Blazor component 如何获得当前的 Crome URL? - How can I get the current Crome URL? 在Startup.cs中使ConfigureServices方法异步 - Make the ConfigureServices method async in Startup.cs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM