將不同的 appsettings.json 加載到 Blazor WebAssembly
我目前正在開發(fā) .NET Standard 2.1 Blazor WebAssembly 應(yīng)用程序。我嘗試將不同的 appsettings.{Environment}.json 配置加載到我的 Window 命名空間 (JavaScript) 中。因此,我遵循這篇博文:https://jkdev.me/blazor-appsettings/到目前為止一切順利:我在我的 wwwroot 目錄中添加了 3 個(gè) appsettings.*.json 文件:應(yīng)用設(shè)置.json:{ "App": { "Message": "Hello World!" }}appsettings.Development.json:{ "App": { "Environment": "Development" }}應(yīng)用設(shè)置.Staging.json:{ "App": { "Environment": "Staging" }}在我的 program.cs Main 方法中,我構(gòu)建了新的配置設(shè)置,如下所示: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; }}進(jìn)一步,我嘗試根據(jù)設(shè)置的環(huán)境變量(在腳本中)加載正確的 appsettings.json,因此我需要像這樣覆蓋 Blazor 啟動(dòng)過程:<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>
查看完整描述