在ASP.NET Core中为开发和发布环境自动设置appsettings.json吗? [英] Automatically set appsettings.json for dev and release environments in asp.net core?

查看:107
本文介绍了在ASP.NET Core中为开发和发布环境自动设置appsettings.json吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在appsettings.json中为数据库连接字符串,webapi位置等定义了一些值,这些值对于开发,暂存和实时环境是不同的.

I've defined some values in my appsettings.json for things like database connection strings, webapi locations and the like which are different for development, staging and live environments.

有没有一种方法可以拥有多个appsettings.json文件(例如appsettings.live.json等),并让asp.net应用根据运行的构建配置知道"要使用哪个文件?

Is there a way to have multiple appsettings.json files (like appsettings.live.json, etc, etc) and have the asp.net app just 'know' which one to use based on the build configuration it's running?

推荐答案

您可以使用条件编译:

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();
}

这篇关于在ASP.NET Core中为开发和发布环境自动设置appsettings.json吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆