Asp.net Net Core 2.0正在查找appsettings.Production.json
我已经创建了两个这样的.net core 2 web应用程序,在此之前没有任何问题 在IISExpress下运行local时运行正常,但当我将调试版本部署到服务器上的IIS文件夹时,我遇到了问题 当我读取配置条目时,找不到它:Asp.net Net Core 2.0正在查找appsettings.Production.json,asp.net,asp.net-core,.net-core,asp.net-core-2.0,Asp.net,Asp.net Core,.net Core,Asp.net Core 2.0,我已经创建了两个这样的.net core 2 web应用程序,在此之前没有任何问题 在IISExpress下运行local时运行正常,但当我将调试版本部署到服务器上的IIS文件夹时,我遇到了问题 当我读取配置条目时,找不到它: _config["MySettings:MyName"]; 文件内容: appsettings.Development.json { "Logging": { "IncludeScopes": false, "LogLevel": { "
_config["MySettings:MyName"];
文件内容:
appsettings.Development.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "Information"
}
},
"MySettings": {
"MyName": "JohnDoe"
}
}
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Trace"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60668/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/security",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApi.ViewerSecurity": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/security",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:60669/"
}
}
}
appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "Information"
}
},
"MySettings": {
"MyName": "JohnDoe"
}
}
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Trace"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60668/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/security",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApi.ViewerSecurity": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/security",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:60669/"
}
}
}
launchSetting.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "Information"
}
},
"MySettings": {
"MyName": "JohnDoe"
}
}
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Trace"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60668/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/security",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApi.ViewerSecurity": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/security",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:60669/"
}
}
}
如果我复制appsettings.Development.json并重命名为appsettings.Production.json,它就会工作
我更改了prod文件中“MyName”的值并将其记录下来。是的,从appsettings.Production.json读取
如何以及为什么?没有在任何地方定义生产。默认的
ConfigurationBuilder
正在查找appsettings..json
文件,因此根据您使用的环境,此文件的名称会发生更改。当您在IIS Express中时,您处于开发中
,当您部署应用程序时,您的环境是生产环境
。这就是为什么需要appsettings.Production.json
调试时,有一个名为
ASPNETCORE\u Environment
的环境变量被设置为Development
,在部署中未设置ASPNETCORE\u Environment
,默认值为Production,将环境更改为Production。我看不到在任何地方设置了任何环境变量。如果未设置ASPNETCORE\u ENVIRONMENT
,它将默认为Production
。这有文档记录吗?@ginalster有,这是链接