简体   繁体   English

覆盖ASP.Net 5中Azure Web App中config.json文件中的配置值

[英]Overriding Configuration Values in config.json file in Azure Web App in ASP.Net 5

I have a ASP.Net 5 application where i have some configuration values stored in config.json file. 我有一个ASP.Net 5应用程序,其中我有一些配置值存储在config.json文件中。 my config.json file is something like this. 我的config.json文件是这样的。

{
  "AppSettings": {
    "SiteEmailAddress": "some@email.com",
    "APIKey": "some_api_key"
  }
}

I am setting up the config.json file to use in Startup.cs file like this. 我正在设置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();
}

And accessing the config settings like this.. 并访问这样的配置设置..

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

Earlier in ASP.Net we can use the Web.Config file to store these Application Settings and override them in App Settings in Azure App Settings section and that works with out any issues. 在ASP.Net早期,我们可以使用Web.Config文件来存储这些应用程序设置,并在“Azure应用程序设置”部分的“应用程序设置”中覆盖它们,这样可以解决任何问题。 But how can i do the same thing in ASP.Net 5 app. 但是我怎么能在ASP.Net 5应用程序中做同样的事情。

How can i override the configuration values in config.json file in the App Settings section in Azure. 如何覆盖Azure中“应用程序设置”部分中config.json文件中的配置值。

Add them as App Settings in Azure, just like you are used to. 将它们添加为Azure中的应用程序设置,就像您以前一样。 For nested config values, use 对于嵌套配置值,请使用

AppSettings:SiteEmailAddress

Etc... (AppSettings here referring to what you used in your config.json, the similarity to Azure App Settings is coincidental) 等等...(AppSettings在这里指的是你在config.json中使用的,与Azure App Settings的相似性是巧合的)

AddEnvironmentVariables() like you have done is required for this to work. 像你所做的AddEnvironmentVariables()是必须的。

Assuming you have appsettings.json, you could add another file appsettings.{Environment}.json, ie appsettings.Production.json. 假设你有appsettings.json,你可以添加另一个文件appsettings。{Environment} .json,即appsettings.Production.json。 Only settings, defined in the production file would override settings in the appsettings.json. 只有生产文件中定义的设置才会覆盖appsettings.json中的设置。 Now, add the following to the constructor of Startup 现在,将以下内容添加到Startup的构造函数中

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true);

Next, you should go to launchSettings.json where all servers are defined and update environment variable to Production. 接下来,您应该去launchSettings.json,其中定义了所有服务器并将环境变量更新为Production。 For example, 例如,

"web": {
  "commandName": "web",
  "environmentVariables": {
    "Hosting:Environment": "Production"
  }

Now deploy to azure. 现在部署到azure。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM