【问题标题】:Net Core 2.0 looking for appsettings.Production.jsonNet Core 2.0 寻找 appsettings.Production.json
【发布时间】:2018-11-23 17:48:12
【问题描述】:

我已经创建了几个这样的 .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 读取。

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

【问题讨论】:

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


    【解决方案1】:

    ConfigurationBuilder 的默认值是寻找appsettings.<EnvironmentName>.json 文件,因此根据您使用的环境,更改此文件的名称。当您在 IIS Express 中时,您在 Development 中,而当您部署应用程序时,您的环境是 Production。这就是为什么你需要appsettings.Production.json

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

    【讨论】:

    • 是什么将环境更改为生产。我没有看到任何环境变量设置在任何地方
    • 如果没有设置ASPNETCORE_ENVIRONMENT,则默认为Production
    • 这有记录吗?
    猜你喜欢
    • 2019-04-10
    • 1970-01-01
    • 2018-09-04
    • 2018-08-03
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2018-04-16
    相关资源
    最近更新 更多