Wil*_*sch 6 c# git appsettings local-files visual-studio-2017
我的应用程序使用appsettings.json进行某些设置.如果存在appsettings.local.json,则应该覆盖appsettings.json以包含它包含的任何设置.到目前为止,没问题.
但我使用git进行版本控制.显然,我不希望其他用户拉下我的本地设置.所以我git忽略appsettings.json.
此外,解决方案中有很多项目.它们共享相同的设置.所以在解决方案级别有一个appsettings.json,所有项目都将它作为链接包含在内.
还不错,除了一件事.为了可以使用,我必须将appsettings.local.json复制到输出目录.但它不应该在版本控制中.因此,如果有人克隆解决方案新鲜,他们将没有它.那应该没问题,但事实并非如此.VS. 说"这个文件应该链接,但它到底在哪里?" 构建错误.
我该怎么处理?
使用v2,这很简单.
appsettings.local.json到您的项目(它应嵌套在主appsettings.json文件下面).appsettings.local.json你的.gitignore在您startup.cs的构造函数内执行以下操作:
public class Startup
{
public IConfigurationRoot Configuration { get; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) //load base settings
.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true) //load local settings
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) //load environment settings
.AddEnvironmentVariables();
Configuration = builder.Build();
}
/*
* rest of your startup.cs
*/
}
Run Code Online (Sandbox Code Playgroud)对于.NET 6,您可以使用它。
还要确保您 gitignoreappsettings.local.json以避免签入密码和机密等敏感信息。
var builder = WebApplication.CreateBuilder(args);
// Add configurations
builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{
config.Sources.Clear();
var env = hostingContext.HostingEnvironment;
config.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) //load base settings
.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true) //load local settings
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) //load environment settings
.AddEnvironmentVariables();
if (args != null)
{
config.AddCommandLine(args);
}
});
Run Code Online (Sandbox Code Playgroud)
对于 .Net Core >2.1,您可以简单地将扩展方法链接ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)到您的 WebHost。下面是一个例子:
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile(
"appsettings.Local.json",
optional: true,
reloadOnChange: true);
})
// ...
Run Code Online (Sandbox Code Playgroud)
当然,忽略 .gitignore 中的 appsettings.Local.json。
不能说我喜欢将这个环境特定的东西添加到源代码中,将其添加为类似的文件似乎更干净appsetting.local.json,然后在你的内部launchSettings.json确保将环境变量添加为本地。
{
"profiles": {
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "local"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
public class Startup
{
public IConfigurationRoot Configuration { get; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) //load base settings
// Avoid this nasty code in your codebase, its oddly specific for a particular environment.
//.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true) //load local settings
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) //load environment settings
.AddEnvironmentVariables();
Configuration = builder.Build();
}
/*
* rest of your startup.cs
*/
}
Run Code Online (Sandbox Code Playgroud)