asp.net-core - 如何在代码中使用 appsettings.Development.json 变量

标签 asp.net-core

我有一个 appsettings.json 和 appsettings.Development.json。我需要根据环境分配 SmtpServer 名称。

appsettings.json 中的配置文件是:

{ 
  "EmailConfiguration": {
   "SmtpServer": "mail.MYDOMAIN.com"
  }
}

在 appsettings.Development.json 中是:
{ 
  "EmailConfiguration": {
    "SmtpServer": "mail.MYLOCAL.com"
  }
}

当我在 Startup ConfigureServices() 中分配配置时,如下所示:
var emailconfig = Configuration.GetSection("EmailConfiguration").Get<EmailConfiguration>();
services.AddSingleton<IEmailConfiguration>(emailconfig);

它总是使用 appsettings.json ('mail.MYDOMAIN.com') 而不是 appsettings.Development.json。

如何修改此代码以使用正确的环境设置?

最佳答案

答案是我需要的只是 2 个不同 appsettings 文件中的环境变量。我的问题是我的配置设置之一中缺少右括号,这意味着 appsettings.Development.json 不会替换 appsettings.json 变量,因为它们不匹配。哎呀……

关于asp.net-core - 如何在代码中使用 appsettings.Development.json 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48066402/

相关文章:

c# - 如何获取 Azure Blob 存储中目录的 "Last Modified"属性

c# - Nginx 作为 Ubuntu 上带有 UseRewriter 的 Asp.Net Core 2.0 Web 应用程序的反向代理

asp.net-core - 在 ASP.NET MVC Core 中,Microsoft.AspNetCore.Razor.Design 和 Microsoft.VisualStudio.Web.CodeGeneration.Design 包有什么作用?

c# - 从 SWT token 获取 claim

linux - 在 Ubuntu 上运行独立的 ASP.NET Core 应用程序

javascript - Signalr Core 1.0.3 - 在页面刷新时保持连接

mysql - 如何使用 MysqlStorage 在版本 1.6 中增加 Hangfire 超时

c# - ASP 核心 : URL Rewrite not working when using AddIISUrlRewrite

authentication - 在asp.net core 3.1中注册基于租户的认证方案

c# - Jwt 代码不适用于 .NET Core 2