You could use AddEnvironmentVariables
property to achieve override appsettings on azure to local settings.
First configure the setting in portal:
Note: The value here is null.
To override nested keys in the App Settings section we can define a variable using the full path SettingsA:PropA
as name or using double underscore SettingsA__PropA
. You could refer to this article.
In local, you could configure as below:
In Startup.cs:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
configuration = builder.Build();
}
public IConfiguration configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<SettingsOptions>(configuration.GetSection("SettingsA"));
}
In appsettings.json:
{"SettingsA": {
"PropA": ["a","b"]
}
}
In HomeController:
private readonly IOptions<SettingsOptions> options;
public HomeController(IOptions<SettingsOptions> options)
{
this.options = options;
}
public IActionResult Index()
{
var value = options.Value;
ViewBag.Index = value.PropA+"success";
return View();
}
In SettingsOption:
public class SettingsOptions
{
public string SettingsA { get; set; }
public string PropA { get; set; }
}
After you publish the project to azure, it will override the PropA value.
For more details about how to read appsetting from asp.net core, please follow this case.