我已经在我appsettings.json的数据库连接字符串,webapi位置等内容中定义了一些值,这些值对于开发,登台和实时环境是不同的.
有没有办法有多个appsettings.json文件(如appsettings.live.json,等等),并让asp.net应用程序'知道'根据它运行的构建配置使用哪一个?
所以我希望能够dotnet在终端上运行(.net core mvc-project)时选择我的环境.我找到了这篇文章,并认为第二个最高答案是一个简洁的解决方法,简而言之:
用以下内容替换Program类主体:
private static readonly Dictionary<string, string> defaults =
new Dictionary<string, string> {
{ WebHostDefaults.EnvironmentKey, "development" }
};
public static void Main(string[] args)
{
var configuration =
new ConfigurationBuilder()
.AddInMemoryCollection(defaults)
.AddEnvironmentVariables("ASPNETCORE_")
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
Run Code Online (Sandbox Code Playgroud)
然后运行:
dotnet run environment=development
dotnet run environment=staging
Run Code Online (Sandbox Code Playgroud)
所以我粘贴了它,它说我需要添加一个使用的声明
using Microsoft.Extensions.Configuration;
仍然收到此错误消息:
'IConfigurationBuilder' does not contain a definition for
'AddCommandLine' and no extension method 'AddCommandLine'
accepting a first argument of …Run Code Online (Sandbox Code Playgroud)