在asp.net核心中为dev和release环境自动设置appsettings.json?
我已经在appsettings.json
定义了一些值,例如数据库连接字符串,webapi位置等,它们对于开发,登台和实时环境是不同的。
有没有办法拥有多个appsettings.json
文件(如appsettings.live.json
等等)并让asp.net应用程序只是’知道’根据它运行的构建配置使用哪一个?
您可以使用条件编译:
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) #if SOME_BUILD_FLAG_A .AddJsonFile($"appsettings.flag_a.json", optional: true) #else .AddJsonFile($"appsettings.no_flag_a.json", optional: true) #endif .AddEnvironmentVariables(); this.configuration = builder.Build(); }
我已经添加了工作环境的快照,因为它花了我几个小时进行研发。
首先添加Launch.Json
文件的密钥。
见下文,我添加了"Development"
作为我的环境。
然后在项目中添加具有相同名称的appSetting文件。
添加了snap,查找以2命名的不同文件。
-
appSettings.Development.Json
-
appSetting.json
最后将它配置为您的StartUp类,如下所示。
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(); }
最后我从终端运行它就像这样。
dotnet run –environment“开发”
发展是我的环境。
您可以在Startup
构造函数中使用环境变量和ConfigurationBuilder
类,如下所示:
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); this.configuration = builder.Build(); }
然后为所需的每个环境创建一个appsettings.xxx.json
文件,其中“xxx”是环境名称。 请注意,您可以将所有全局配置值放在“普通” appsettings.json
文件中,并仅将特定于环境的内容放入这些新文件中。
现在,您只需要一个名为ASPNETCORE_ENVIRONMENT
的环境变量, ASPNETCORE_ENVIRONMENT
包含一些特定的环境值(“实时”,“分段”,“生产”等等)。 您可以在开发环境的项目设置中指定此变量,当然还需要在临时和生产环境中设置它。 你在那里的方式取决于这是什么样的环境。
更新:我刚刚意识到你想根据你当前的构建配置选择appsettings.xxx.json
。 使用我提出的解决方案无法实现这一点,我不知道是否有办法做到这一点。 然而,“环境变量”方式有效,并且可能是您的方法的一个很好的替代方案。
只是.NET核心2.0用户的更新,您可以在调用CreateDefaultBuilder
后指定应用程序配置:
public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration(ConfigConfiguration) .UseStartup() .Build(); static void ConfigConfiguration(WebHostBuilderContext ctx, IConfigurationBuilder config) { config.SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("config.json", optional: false, reloadOnChange: true) .AddJsonFile($"config.{ctx.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true); } }
在ASP.NET Core中,您应该使用EnvironmentVariables而不是构建配置来正确使用appsettings.json
Right click on you project > Properties > Debug > Environment Variables
ASP.NET Core将采用适当的appsettings.json文件。 如果你这样做@Dmitry的方式,你会遇到问题,例如。 在Azure上覆盖appsettings.json值时。
您可以在ASPNETCORE_ENVIRONMENT
中将配置名称添加为launchSettings.json
,如下所示
"iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:58446/", "sslPort": 0 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "environmentVariables": { ASPNETCORE_ENVIRONMENT": "$(Configuration)" } } }