我已经创建了几个这些.net core 2 Web应用程序,直到这个都没有问题。
在IISExpress下本地运行可正常运行,但是当我将调试版本部署到服务器上的IIS文件夹时,出现了问题。
当我读取配置条目时,找不到它:
_config["MySettings:MyName"];
文件内容:
appsettings.Development.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "Information"
}
},
"MySettings": {
"MyName": "JohnDoe"
}
}
appsettings.json
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Trace"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
launchSetting.json
{
"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,它将起作用。
我在产品文件中更改了“ MyName”的值并记录下来。是的,从appsettings.Production.json中读取。
如何以及为什么?生产在任何地方都没有定义。
最佳答案
ConfigurationBuilder
的默认值是查找appsettings.<EnvironmentName>.json
文件,因此根据您使用的环境,将更改此文件的名称。当您使用IIS Express时,您将使用Development
;当您部署应用程序时,您的环境将是Production
。这就是为什么您需要appsettings.Production.json
的原因。
调试时,有一个名为ASPNETCORE_ENVIRONMENT
的环境变量,该变量设置为Development
,并且在您的部署中未设置ASPNETCORE_ENVIRONMENT
的情况下,默认变量是Production
关于asp.net - Net Core 2.0寻找appsettings.Production.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50849193/