Wednesday, August 23, 2017

ASP.NET Core 2.0–Help, where is my Configuration?

In ASP.NET Core 1.x the Configuration was initialized in the Startup class:

If you create a new project in ASP.NET Core 2.0, your Startup class is almost empty and you cannot find any Configuration related code anywhere. Still if you run your application, the configuration is loaded and everything is working as expected.

So where is the magic?

The Configuration is no longer found in the Startup class but is loaded and executed from the Program.cs file where it is part of the WebHost.CreateDefaultBuilder method.

This method does a lot of stuff out-of-the box including loading configuration data from the following sources:

  • appsettings.json and appsettings.{environment}.json (e.g. appsettings.Development.json)
  • User secrets
  • Environment variables
  • Command-line arguments

This explains the magic. But what if you want to change this? No worries, you can use the ConfigureAppConfiguration method to change the configuration.

If you need the Configuration data in your Startup class, you can simply inject it through the constructor:

No comments: