
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Configure ASP.NET Core Applications
During the development of an application, and even after it’s built, you often need to change various settings that control how the application behaves. Configuration refers to the external values that control an application's behavior, consisting of settings and parameters that the application uses at runtime.
The best practice regarding storing configuration values is outside the application, instead of hard-coding them in the source code. You don’t want to recompile and restart the application every time you make a change to the configuration. There are also security implications. You don’t want to store the database connection strings or passwords in the source code as plain-text.
ASP.NET Core makes it very easy to manage the configuration for your application. You can configure the settings via various sources such as JSON files, environment variables, and command-line arguments. The framework takes care of loading these settings and makes them available via a unified configuration object. It supports multiple file formats such as JSON, XML, YAML, etc. You can also create your own custom configuration provider. The framework also allows you to override the settings.
Here are various configuration providers that ASP.NET Core supports:
Settings files such as appsettings.json
Environment variables
Azure Key Vault
Azure App Configuration
Command-line arguments
Custom providers, installed or created
Directory files
In-memory .NET objects
The CreateDefaultBuilder() method in the Program class provides the default configuration.
public class Program{ public static void Main(string[] args){ CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
This method loads the configuration in the following order:
ChainedConfigurationProvider
appsettings.json
appsettings.Environment.json
App secrets
Environment variables
Command-line arguments
If you add any more configuration providers later, they override the previous settings.
Environment Variables
The EnvironmentVariablesConfigurationProvider loads the configuration values from the environment variables as key-value pairs. This is done after reading appsettings.json, appsettings.Environment.json, and user secrets. Hence, the settings from the environment variables override those that came from appsettings.json files and the user secrets.
On windows, you can set the environment variables using the set or setx command.
set CONN_STR="Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"
Command-line Variables
The configuration settings set via command-line override all other configuration providers. It uses the CommandLineConfigurationProvider to load the configuration from command-line arguments as key-value pairs. For example:
dotnet run CONN_STR="Server=myServerAddress; Database=myDataBase; User Id=myUsername; Password=myPassword;"