Asp.net
Net Core 2.0 尋找 appsettings.Production.json
我已經創建了幾個這樣的 .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" } }應用設置.json
{ "Logging": { "IncludeScopes": false, "Debug": { "LogLevel": { "Default": "Trace" } }, "Console": { "LogLevel": { "Default": "Warning" } } } }啟動設置.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 它可以工作。
我在 prod 文件中更改了“MyName”的值並將其記錄下來。是的,從 appsettings.Production.json 讀取。
如何以及為什麼?生產在任何地方都沒有定義。
預設
ConfigurationBuilder是查找appsettings.<EnvironmentName>.json文件,因此根據您使用的環境,更改此文件的名稱。當您在 IIS Express 中時,您就在其中,Development而當您部署應用程序時,您的環境是Production. 這就是為什麼你需要appsettings.Production.json.當您調試時,有一個名為的環境變數
ASPNETCORE_ENVIRONMENT設置為,Development並且在您的部署中ASPNETCORE_ENVIRONMENT未設置,預設值為Production