Asp.net core 发布未从appsettings.production.json获取连接字符串
我无法使用appsettings.production.json文件中的连接字符串切换到已发布的ASP.net核心应用程序 我已将launchSettings设置为在开发和生产配置文件中使用ASPNETCORE_环境变量。当我切换配置文件并在VisualStudio中运行它们时,我的连接字符串会正确更改 当我在Ubuntu服务器上运行发布的应用程序时,它不会切换。我已在服务器上将ASPNETCORE_环境变量设置为“Production”。我还验证了appSettings.json和appSettings.production.json都存在于应用程序的根目录中 My appsettings.json文件:Asp.net core 发布未从appsettings.production.json获取连接字符串,asp.net-core,Asp.net Core,我无法使用appsettings.production.json文件中的连接字符串切换到已发布的ASP.net核心应用程序 我已将launchSettings设置为在开发和生产配置文件中使用ASPNETCORE_环境变量。当我切换配置文件并在VisualStudio中运行它们时,我的连接字符串会正确更改 当我在Ubuntu服务器上运行发布的应用程序时,它不会切换。我已在服务器上将ASPNETCORE_环境变量设置为“Production”。我还验证了appSettings.json和appSet
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"DefaultConnection": "server=localhost;user id=root;password=dev;database=testdb;sslmode=none",
}
}
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;user id=root;password=prod;database=testdb;sslmode=none"
}
}
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50824/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "home/index",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express (Production)": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "home/index",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
}
我的appsettings.production.json文件:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"DefaultConnection": "server=localhost;user id=root;password=dev;database=testdb;sslmode=none",
}
}
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;user id=root;password=prod;database=testdb;sslmode=none"
}
}
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50824/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "home/index",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express (Production)": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "home/index",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
}
My launchSettings.json文件:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionStrings": {
"DefaultConnection": "server=localhost;user id=root;password=dev;database=testdb;sslmode=none",
}
}
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;user id=root;password=prod;database=testdb;sslmode=none"
}
}
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50824/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "home/index",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express (Production)": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "home/index",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
}
My Startup.cs:
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);
if (env.IsEnvironment("Development"))
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
Console.WriteLine("Root Path: " + env.ContentRootPath);
Console.WriteLine("Connection String: " + Configuration.GetConnectionString("DefaultConnection"));
}
我已经提到了这些问题,但运气不好:
事实证明,中的“注意事项”非常重要:
在Windows和macOS上,指定的环境名称为大小写
不敏感的无论您将变量设置为开发还是
发展或发展的结果将是相同的。然而,
默认情况下,Linux是区分大小写的操作系统。环境变量,文件
名称和设置应采用区分大小写的最佳做法
主要是“Linux默认情况下是区分大小写的操作系统”!!!!!哎呀:)
一旦我将环境变量改为“production”而不是“production”,它就工作了
进一步解释:
关键在于理解Startup.cs启动方法中的这行代码:
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
它用您的环境变量替换{env.EnvironmentName},因此如果您在linux中操作,它需要与您的文件名完全匹配。在我的例子中是“appSettings.production.json”,所以ASPNETCORE_环境必须是“production”