英文:
.net core 6 publish profiles to replace appsettings with visual studio
问题
我正在为.NET Core 6控制台应用程序编写代码,我想在发布时将appsettings.json
文件替换为appsettings.envname.json
,并根据发布配置使用不同的appsettings。我该如何在Visual Studio中实现这个目标?
英文:
I am writing a .NET core 6 console application for which I would like to replace the appsettings.json
file to appsettings.envname.json
whenever I publish for envname
.
The file should be named appsettings.json
basically based on the publish profile I would like to use a different appsettings.
How can I do that in Visual Studio?
答案1
得分: 1
在控制台应用程序中,您可以在Program.cs文件中编写以下内容:
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{environmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
请注意,这是代码的翻译部分。
英文:
In console application you can write into your Program.cs:
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{environmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论