c# - 重写 ASP.Net 5 中 Azure Web App 的 config.json 文件中的配置值

标签 c# asp.net azure asp.net-core

我有一个 ASP.Net 5 应用程序,其中有一些配置值存储在 config.json 文件中。我的 config.json 文件是这样的。

{
  "AppSettings": {
    "SiteEmailAddress": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e5968a8880a58088848c89cb868a88" rel="noreferrer noopener nofollow">[email protected]</a>",
    "APIKey": "some_api_key"
  }
}

我正在设置 config.json 文件以在 Startup.cs 文件中使用,如下所示。

public static IConfigurationRoot Configuration;

public Startup(IApplicationEnvironment appEnv) {

    var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();
    Configuration = builder.Build();
}

并像这样访问配置设置..

var email = Startup.Configuration["AppSettings:SiteEmailAddress"];

在 ASP.Net 早期,我们可以使用 Web.Config 文件来存储这些应用程序设置,并在 Azure 应用程序设置部分的应用程序设置中覆盖它们,这样就不会出现任何问题。但我怎样才能在 ASP.Net 5 应用程序中做同样的事情。

如何覆盖 Azure 中“应用程序设置”部分的 config.json 文件中的配置值。

最佳答案

将它们添加为 Azure 中的应用程序设置,就像您习惯的那样。 对于嵌套配置值,请使用

AppSettings:SiteEmailAddress

等等...(这里的AppSettings指的是你在config.json中使用的内容,与Azure App Settings相似纯属巧合)

像您所做的那样,需要 AddEnvironmentVariables() 才能正常工作。

关于c# - 重写 ASP.Net 5 中 Azure Web App 的 config.json 文件中的配置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33855298/

相关文章:

c# - 从列表中选择后对象仍然链接(引用)?

javascript - Jquery 未捕获错误

MS Teams 中的 Azure 聊天机器人

c# - 在 WPF 中很好地控制缩放和添加元素

c# - 防止 Excel 退出

c# - NullReferenceException 而涉及的对象是有效的

asp.net - 寻找代码来呈现显示对象 View 的表单

javascript - 如何使用 Asp.net 和 Jquery 动态显示评论(如 Stack Overflow)?

azure - Azure 有多少个核心?

c# - 更新 Windows Azure 移动服务表条目