代码之家  ›  专栏  ›  技术社区  ›  Aman B

.Net core 2控制台appsettings转换

  •  6
  • Aman B  · 技术社区  · 7 年前

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

    • 应用程序设置。json
    • 应用程序设置。测验json

    我发现以下代码适用于asp。净核心:

    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

    1 回复  |  直到 7 年前
        1
  •  2
  •   Aman B    7 年前

    无法找到任何其他内容,因此暂时使用预处理器指令

    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();
    }