我创建了几个 .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读取。
为什么?Production没有在任何地方定义。
ASPNETCORE_ENVIRONMENT
,它将默认为Production
。 - Kahbazi