简体   繁体   English

Dotnet Core 使用哪个 appsettings.json 文件作为默认环境或父文件?

[英]Which appsettings.json file does Dotnet Core use as default, the environment or parent file?

What dictates which .json file the .net core clr / runtime uses?什么决定了 .net core clr/runtime 使用哪个 .json 文件? For example if there's a connection string in both.例如,如果两者都有一个连接字符串。 I can't find an answer anywhere.我在任何地方都找不到答案。

var env = builderContext.HostingEnvironment;
                config.AddJsonFile("appsettings.json", optional: false)
                      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

Thanks!谢谢!

There's no "default" order.没有“默认”顺序。 .NET Core will use the files specified in your code. .NET Core 将使用您的代码中指定的文件。 There's nothing special about appsettings.json or the Json configuration provider either. appsettings.json或 Json 配置提供程序也没有什么特别之处。 As far as .NET Core is concerned they just providers就 .NET Core 而言,它们只是提供者

This code specifies that :此代码指定:

  1. The required appsettings.json file is loaded first.首先加载所需的appsettings.json文件。 If it doesn't exist the app fails如果它不存在,则应用程序失败
  2. The optional environment-specific file is loaded next, which may override the first one.接下来加载可选的特定于环境的文件,它可能会覆盖第一个。 If it doesn't exist, nothing is loaded如果它不存在,则不加载任何内容

Even the expression $"appsettings.{env.EnvironmentName}.json" has no special meaning.甚至表达式$"appsettings.{env.EnvironmentName}.json"也没有特殊含义。 It's a string interpolation expression that replaces the {env.EnvironmentName} placeholder with the value of the env.EnvironmentName property.它是一个字符串插值表达式,用env.EnvironmentName属性的值替换{env.EnvironmentName}占位符。

The method call error specifies the order, with later files or providers overriding earlier ones.方法调用错误指定顺序,后面的文件或提供者覆盖前面的文件或提供者。

You could add a command-line or EF Core provider after the JSON files to override them with values stored in the database or specified in the command line.您可以在 JSON 文件之后添加命令行或 EF Core 提供程序以使用存储在数据库中或在命令行中指定的值覆盖它们。

This is explained in the documentation and tutorials, eg Configuration in ASP.NET Core or the Essential .NET - Configuration in .NET Core but it's so different from how the Full framework works that it takes a bit of getting used to.这在文档和教程中进行了解释,例如ASP.NET Core 中的配置Essential .NET - .NET Core 中的配置,但它与完整框架的工作方式大不相同,需要一点时间来适应。

Config is loaded in source-order.配置按源顺序加载。 In other words, each type of config you specify is loaded in the order you add it in your Startup.cs or whatever.换句话说,您指定的每种类型的配置都按照您在Startup.cs或其他文件中添加的顺序加载。 Here, since appsettings.json is specified before appsettings.{env.EnvironmentName}.json , it will be loaded first, followed by the environment-specific version, if it exists.在这里,由于appsettings.jsonappsettings.{env.EnvironmentName}.json之前指定,它将首先加载,然后是特定于环境的版本(如果存在)。

No matter your config sources, everything ends up in the ConfigurationRoot dictionary.无论您的配置来源如何,所有内容都最终出现在ConfigurationRoot字典中。 If multiple config sources specify the same key, then the last one in wins.如果多个配置源指定相同的键,则最后一个获胜。 In other words, config sources added later override those added earlier.换句话说,后来添加的配置源会覆盖之前添加的配置源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM