为 ASP Core 1 应用中的用户自定义 appsetings.json
Customize appsetings.json for user in ASP Core 1 app
我的目标是让 appsettings.json
文件具有生产配置,并有可能为每个开发人员定制它,例如使用本地连接字符串。所以它不类似于 transform web.config 机制,我不想依赖于 bulid 配置。谁能为此目标提供解决方案?
在我过去的一个项目中,我们这样做了:我们将所有配置信息存储在自定义 config.xml 中,并将其解析为自定义结构。 Web.config 仅包含服务器配置。每个开发人员都有自己的配置文件副本和自己的数据。解决方案是应用程序使用路径中的配置文件,该路径在 windows 中的环境路径中通过 Environment.GetEnvironmentVariable("key")
.
指定
有没有人比我的想法更好?
这就是我管理配置的方式:请参阅代码中的注释
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json"); // this one has default configuration
// this file name is added to my gitignore so it won't get committed,
// I keep local dev configuration there
builder.AddJsonFile("appsettings.local.overrides.json", optional: true);
if (env.IsDevelopment())
{
// This reads the configuration keys from the secret store.
// if you need a more secure place for dev configuration use usersecrets
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
// the order in which config sources is added is important, a source added later
// will override the same settings from a source added before
// environment variables is usually for production and therefore added last to give it higher priority
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
我的目标是让 appsettings.json
文件具有生产配置,并有可能为每个开发人员定制它,例如使用本地连接字符串。所以它不类似于 transform web.config 机制,我不想依赖于 bulid 配置。谁能为此目标提供解决方案?
在我过去的一个项目中,我们这样做了:我们将所有配置信息存储在自定义 config.xml 中,并将其解析为自定义结构。 Web.config 仅包含服务器配置。每个开发人员都有自己的配置文件副本和自己的数据。解决方案是应用程序使用路径中的配置文件,该路径在 windows 中的环境路径中通过 Environment.GetEnvironmentVariable("key")
.
有没有人比我的想法更好?
这就是我管理配置的方式:请参阅代码中的注释
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json"); // this one has default configuration
// this file name is added to my gitignore so it won't get committed,
// I keep local dev configuration there
builder.AddJsonFile("appsettings.local.overrides.json", optional: true);
if (env.IsDevelopment())
{
// This reads the configuration keys from the secret store.
// if you need a more secure place for dev configuration use usersecrets
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
// the order in which config sources is added is important, a source added later
// will override the same settings from a source added before
// environment variables is usually for production and therefore added last to give it higher priority
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}