.Net core 2 console appsettings 转换
.Net core 2 console appsettings transform
我正在尝试将 appsettings 转换添加到 .net core 2 控制台应用程序,例如
- appSettings.json
- appSettings.Test.json
- appSettings.Prod.json
我发现以下代码适用于 asp.net 核心:
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
。
任何帮助将不胜感激
找不到其他任何东西,所以现在使用预处理器指令
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();
}
我正在尝试将 appsettings 转换添加到 .net core 2 控制台应用程序,例如
- appSettings.json
- appSettings.Test.json
- appSettings.Prod.json
我发现以下代码适用于 asp.net 核心:
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
。
任何帮助将不胜感激
找不到其他任何东西,所以现在使用预处理器指令
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();
}