Net Core 2.0正在寻找appsettings.Production.json

时间:2018-06-14 03:30:44

标签: asp.net asp.net-core .net-core asp.net-core-2.0

我创建了几个.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就可以了。

我在prod文件中更改了“MyName”的值并记录了它。是的,从appsettings.Production.json读取。

如何以及为何?生产没有在任何地方定义。

1 个答案:

答案 0 :(得分:12)

ConfigurationBuilder的默认值正在查找appsettings.<EnvironmentName>.json文件,因此根据您使用的环境,此文件的名称会更改。当您使用IIS Express时,您处于Development状态,部署应用程序时,您的环境为Production。这就是您需要appsettings.Production.json

的原因

当您调试时,有一个名为ASPNETCORE_ENVIRONMENT的环境变量设置为Development,而在部署中未设置ASPNETCORE_ENVIRONMENT,默认值为Production

相关问题