This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14070|Reply: 7

[ASP.NET] ASP.NET Detailed explanation of Configuration priorities in Core(10).

[Copy link]
Posted on 3/2/2022 4:15:38 PM | | | |
ASP.NET Core project starts, the default execution order is: Host Host -> Read Configuration -> Log Settings -> Registration Service (DI) -> Add Middleware -> WebHost Listening -> Background Work Startup.

Configuration loading and reading are at the very beginning of the startup process. Microsoft's profile in ASP.NET Core:The hyperlink login is visible.

Review:

ASP.NET Detailed explanation of the Middleware middleware of Core (9).
https://www.itsvse.com/thread-9647-1-1.html

ASP.NET Detailed explanation of Core Middleware
https://www.itsvse.com/thread-8126-1-1.html

ASP.NET pit of the default parameters of the Swagger UI in Core(8).
https://www.itsvse.com/thread-9640-1-1.html

ASP.NET Core (7) In-depth analysis of the framework source code
https://www.itsvse.com/thread-9601-1-1.html

ASP.NET Core (VI) DI manually obtains the method of injecting objects
https://www.itsvse.com/thread-9595-1-1.html

ASP.NET Core (five) is based on CAP distributed transactions
https://www.itsvse.com/thread-9593-1-1.html

ASP.NET Core(4) filter unified ModelState model validation
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html
Host.CreateDefaultBuilder method, provide the default configuration for the app in the following order:

  • ChainedConfigurationProvider: Add an existing one as a source. In the default configuration example, add the host configuration and set it as the first source for the application configuration.
  • Use appsettings.json appsettings.json.
  • Use the JSON configuration provider via appsettings: json provided. For example, appsettings. Production.json and appsettings. Development.json。
  • App secrets when the app runs in the environment.
  • Use environment variables configuration providers to provide through environment variables.
  • Using the command line configuration provider is provided through command line parameters.


The source code is as follows:

Source code address:The hyperlink login is visible.



As you can see from the code, the program acquisition configuration priority is:appsettings.json -> appsettings.environment.json -> environment variables -> command-line arguments。 We test according to priority.

Create a new console method to return all configuration information, the code is as follows:


First, appsettings.json the configuration file, as follows:

New appsettings. Test.json configuration, as follows:

Try starting the project and see the configuration of WebConfig:Name and WebConfig:Date, as shown in the following image:

{"Key":"WebConfig:Name","Value":"itsvse.com"},{"Key":"WebConfig:Date","Value":"2021"}


Locate Properties ->launchSettings.jsonfile, modify the ASPNETCORE_ENVIRONMENT environment configuration to Test, as follows:

At this time, the procedurewill read the appsettings. Test.json configuration, try to restart the project, findWebConfig:Name 已经覆盖了, as shown in the figure below:

{"Key":"WebConfig:Name","Value":"itsvse.com test"},{"Key":"WebConfig:Date","Value":"2021"}



Modify the launchSettings.json file again and set the value of WebConfig:Name via the environment variable, the code is as follows:

Note: Modify the value of WebConfig:Name for the environment variable, the variable name is: WebConfig__Name (The middle is separated by a double underscore

Try restarting the project and find the value of WebConfig:Name,It has been overwritten by the value set by the environment variable, as shown in the figure below:



Try to modify the default value via the command line, and start the command as follows:

As shown below:



Test the priority of configuration keys with practice, and that's it.





Previous:CentOS 7 prohibits an IP from accessing the server
Next:Kirin System uses YUM source to install the OceanBase error solution
Posted on 3/2/2022 7:59:19 PM |
Learned to learn。。。。。
 Landlord| Posted on 3/29/2023 6:25:09 PM |
Linux system configuration environment variables

Temporary settings



Permanent setting
 Landlord| Posted on 5/12/2024 5:41:57 PM |
ASP.NET Core (twenty-eight) stores application secrets in development
https://www.itsvse.com/thread-10768-1-1.html
 Landlord| Posted on 7/12/2024 1:20:29 PM |
ASP.NET Core (twenty-one) configuration options are the difference between AddOptions and Configure
https://www.itsvse.com/thread-10614-1-1.html
 Landlord| Posted on 5/29/2025 10:01:37 AM |
If usedSystem environment variables that are not working properly, if you debug code in Visual Studio, keep Visual Studio in mindEnvironment variables that exist at startup are cached- Instead of the environment variables that exist when you click "debug"!

Therefore, you may have toRestart Visual Studio to see any changes to the environment

 Landlord| Posted on 7/31/2025 4:25:46 PM |
Set the environment via command line parameters:
 Landlord| Posted on 8/1/2025 1:39:21 PM |
Environment variable modification collection

Set up
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com