[英]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.json和appsettings.{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.