I am trying to use custom configuration for a web app that I intent to host on Azure. The configuration should be overridable by Environment variables so that I can change it on Azure portal.
I tried with following code but it does not work - details below
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("config.json", optional:true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AppSettings>(Configuration);
}
In the controller,
public class HomeController : Controller
{
private IOptions<AppSettings> Configuration;
public HomeController(IOptions<AppSettings> configuration)
{
Configuration = configuration;
}
public async Task<IActionResult> Index()
{
string location = Configuration.Value.Location;
...
}
The default config.json file looks like,
{
"AppSettings": {
"Location" : "Singapore"
}
}
On Azure Portal, under app settings I have assigned value to AppSettings:Location to US and I am expecting US value in the controller.
Locally, in ConfigureServices I can see the value as Singapore but in the controller action Index it is null.
Am I missing something here?