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

查看:27
本文介绍了在asp.net核心中为开发和发布环境自动设置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核心中为开发和发布环境自动设置appsettings.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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