asp.net - Net Core 2.0寻找appsettings.Production.json

标签 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,它将起作用。

我在产品文件中更改了“ 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/

相关文章:

c# - 为什么安全印章不会注销用户?

c# - 无法识别 ASP.NET Core ViewComponent 的 Invoke() 方法调用

pdf - .NET Core pdf下载器 "No output formatter was found for content types '应用程序/pdf'...”

.net-core - .NET 核心 SqlException :an error occurred during the pre-login handshake

.net-core - 在 .NET Core 2 中使用 Microsoft.AspNetCore.All 时可能会遇到哪些问题?

c# - 如何在json单引号中编写c#字符串

asp.net - 用于显示 OLAP 多维数据集的自由控件

c# - 基于枚举值的样式

c# - ASP.NET 核心 2.1 : Navigating back to a page after an HTTP POST fails validation displays a browser error

c# - Catch Microsoft.Extensions.Caching.Redis 连接失败?