简体   繁体   English

加载不同的 appsettings.json 到 Window object 在 Z98AD8B3C99B3CA16F1F7FA84EE64C44F 中

[英]Load different appsettings.json into Window object in Blazor WebAssembly

I'm currently working on a .NET Standard 2.1 Blazor WebAssembly application.我目前正在开发 .NET 标准 2.1 Blazor WebAssembly 应用程序。 I try to load different appsettings.{Environment}.json configurations into my Window namespace (JavaScript).我尝试将不同的 appsettings.{Environment}.json 配置加载到我的 Window 命名空间(JavaScript)中。

Therefore I follow along this blog post:因此,我遵循这篇博文:

https://jkdev.me/blazor-appsettings/ https://jkdev.me/blazor-appsettings/

So far so good: I added 3 appsettings.*.json files into my wwwroot directory:到目前为止一切顺利:我在我的 wwwroot 目录中添加了 3 个 appsettings.*.json 文件:

appsettings.json: appsettings.json:

{
  "App": {
    "Message": "Hello World!"
  }
}

appsettings.Development.json: appsettings.Development.json:

{
  "App": {
    "Environment": "Development"
  }
}

appsettings.Staging.json: appsettings.Staging.json:

{
  "App": {
    "Environment": "Staging"
  }
}

In my program.cs Main method I build my new configuration settings like this:在我的 program.cs Main 方法中,我构建了新的配置设置,如下所示:

public class Program
{
    public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        builder.RootComponents.Add<App>("app");

        ConfigureServices(builder.Services);

        await builder.Build().RunAsync();
    }

    private static void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(
             provider =>
                 {
                     var config = provider.GetService<IConfiguration>();

                     return config.GetSection("App").Get<AppConfiguration>();
                 });

    }
}

public class AppConfiguration
{
    public string Environment { get; set; }
}

Further on I try to load the correct appsettings.json according to a set environment variable (here in a script), therefore I need to override the Blazor boot process like this:此外,我尝试根据设置的环境变量(在脚本中)加载正确的 appsettings.json,因此我需要像这样覆盖 Blazor 引导过程:

<script src="_framework/blazor.webassembly.js" autostart="false"></script>
<script>
    const environmentName = 'Staging';
    Blazor.start({
        loadBootResource: function(type, name, defaultUri, integrity) {
            // Adds a custom HTTP header to the outbound requests
            // To retain the default integrity checking behavior, it's necessary to pass through the 'integrity' parameter
            return fetch(defaultUri,
                {
                    cache: 'no-cache',
                    integrity: integrity,
                    headers: { 'blazor-environment': environmentName }
                });
        }
    });
</script>

Unfortunately this coding doesn't work - I always get an error:不幸的是,这种编码不起作用 - 我总是得到一个错误:

在此处输入图像描述

Do you know how to correctly override the Blazor.start boot process in JavaScript?您知道如何正确覆盖 JavaScript 中的 Blazor.start 引导过程吗?

Unfortunately there is not much documentation out there yet.不幸的是,还没有太多的文档。

Do you know how to load different appsettings.*.json configurations in Blazor WASM?你知道如何在 Blazor WASM 中加载不同的 appsettings.*.json 配置吗?

App settings are per environment.应用程序设置是每个环境。 This means you need to set the environment variable on server side either by setting the ASPNETCORE_ENVIRONMENT environment variable or the blazor-environment response header: https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/environments?view=aspnetcore-3.1这意味着您需要通过设置ASPNETCORE_ENVIRONMENT环境变量或blazor 环境响应 header: https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/environment在服务器端设置环境变量?view=aspnetcore-3.1

The Blazor Wasm boot script will load the appsettings.json and appsettings.{Environment}.json file corresponding to the blazor-environment received and populate the WebAssemblyHostBuilder.Configuration with data in thoose files. Blazor Wasm 启动脚本将加载appsettings.jsonappsettings.{Environment}.json文件对应于blazor-environment .收到的数据并使用WebAssemblyHostBuilder.Configuration文件中的数据填充

You can then configuration to your components, services or js.然后您可以配置到您的组件、服务或 js。
https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/configuration?view=aspnetcore-3.1 https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/configuration?view=aspnetcore-3.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Blazor WebAssembly 为特定环境加载不同的脚本 - Blazor WebAssembly load different scripts for specific Environment 从 appsettings.json 获取设置到 Blazor 中的 afterStarted(options) | Blazor 服务器 - Getting settings from appsettings.json to afterStarted(options) in Blazor | Blazor Server .Net Core 2.1-在JavaScript文件中阅读appsettings.json - .Net Core 2.1 - Read appsettings.json in javascript file Visual Studio Mac appsettings.json允许使用简洁的JavaScript - Visual Studio Mac appsettings.json allow unobtrusive javascript Blazor WebAssembly 中 Window /Tab 关闭事件上的自动注销用户 - Automatic logout user on Window /Tab Close Event in Blazor WebAssembly Blazor WebAssembly 中的 JSRuntime 在首次加载时阻止 DOM 更新 - JSRuntime in Blazor WebAssembly blocking DOM updates on first load Asp.Net 核心剃刀 - 如何在 _Layout.cshtml 文件中使用 appsettings.json 值? - Asp.Net core razor - how to use appsettings.json value in _Layout.cshtml file? Blazor WebAssembly 环境变量 - Blazor WebAssembly Environment Variables Blazor Webassembly JS 互操作 - “Microsoft.JSInterop.JSException:在‘窗口’中找不到‘WriteCookie’。” - Blazor Webassembly JS Interop - “Microsoft.JSInterop.JSException: Could not find 'WriteCookie' in 'window'.” Blazor 网络组件:canvas.toDataUrl()? - Blazor webassembly: canvas.toDataUrl()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM