【问题标题】:Blazor. How can I get current url in the Startup class (method ConfigureServices)?布雷泽。如何在 Startup 类(方法 ConfigureServices)中获取当前 url?
【发布时间】:2020-06-14 20:33:46
【问题描述】:

我正在使用 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();

我有一个例外: InvalidOperationException:“RemoteNavigationManager”尚未初始化。

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

【问题讨论】:

  • 别以为你能做到。在有人调用它之前,该应用程序不知道它正在运行的域的任何信息,因此 NavigationManager 无法给你任何信息,直到它在请求中被调用。如果您在请求级别检查初始化怎么办?如果收到请求时未设置 BaseAddress,则使用 NavigationManager 进行设置。
  • 放入配置?
  • 如果是服务器端的,HttpContext.Current.Request 会给你吗?
  • @Gusman 是的,你说得对。需要在第一次请求时以某种方式进行。也许重新初始化 dbrequests,我现在不确定最好的方法。

标签: c# asp.net-core .net-core blazor


【解决方案1】:

我托管了 Blazor WASM,我的服务器需要主机 uri 才能将其作为回调参数提供给第 3 方。

如果您在服务器上使用.razor 文件,您可以使用:

NavigationManager.BaseUri

但就我而言,在 API 调用中,我使用了请求信息来获取所需的 uri:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • Blazor 服务器端
    • IWebAssemblyHostEnvironment 仅在客户端可用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 2019-09-23
    • 2010-12-23
    • 1970-01-01
    相关资源
    最近更新 更多