【问题标题】:.Net core 2 console appsettings transform.Net core 2 控制台应用设置转换
【发布时间】:2017-10-30 11:27:20
【问题描述】:

我正在尝试将 appsettings 转换添加到 .net core 2 控制台应用程序,例如

  • appSettings.json
  • appSettings.Test.json
  • appSettings.Prod.json

我发现以下代码适用于 asp.net core:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: 
true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: 
true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

但是,我不知道如何获取env.EnvironmentName,因为控制台应用程序中没有IHostingEnvironment

任何帮助将不胜感激

【问题讨论】:

标签: appsettings .net-core-2.0


【解决方案1】:

找不到其他东西,所以暂时使用预处理器指令

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if

public Startup()
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: 
true)
#if RELEASE
        .AddJsonFile($"appsettings.Release.json", optional: 
true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 2019-08-15
    • 2016-11-02
    • 2016-12-07
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多