In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the configurations in ASP.NET Core". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the configurations in ASP.NET Core"?
Background
ASP.NET Core provides a flexible, extensible, key-based configuration system. But the configuration system is independent of ASP.NET Core is part of the Microsoft.Extensions class library. It can be used in any type of application.
1. Read configuration in the form of key-value pairs
{"Position": {"Title": "Editor", "Name": "Joe Smith"}, "MyKey": "My appsettings.json Value", "Logging": {"LogLevel": {"Default": "Information", "Microsoft": "Warning" "Microsoft.Hosting.Lifetime": "Information"}, "AllowedHosts": "*"}
Add the following test code to the ConfigureServices method:
Var myKeyValue = Configuration ["MyKey"]; var title = Configuration ["Position:Title"]; var name = Configuration ["Position:Name"]; var defaultLogLevel = Configuration ["Logging:LogLevel:Default"]
2. Multi-environment configuration
Using the default configuration, EnvironmentVariablesConfigurationProvider loads the configuration from the environment variable key-value pair after reading the appsettings.json, appsettings.Environment.json, and Secret Manager. Therefore, the key values read from the environment replace the values read from appsettings.json, appsettings.Environment.json, and Secret Manager. The environment variable set in launchSettings.json, and the environment variable set in launchSettings.json replaces the variable set in the system environment.
3. Read structured configuration data
Add a class TestSubSectionConfig that corresponds to the subsection node in the configuration file
Public class TestSubSectionConfig {public string SubOption1 {get; set;} public string SubOption2 {get; set;}}
Add the following test code to the ConfigureServices method:
/ / use GetSection to parse the section of the configuration file var subsectionOptions = Configuration.GetSection ("subsection") .Get (); var suboption2 = subsectionOptions.SubOption2; Console.WriteLine ($"subsection:suboption2: {suboption2}")
If you need to use it in Controller, you can use dependency injection:
Register the configuration item in ConfigureServices.
Public void ConfigureServices (IServiceCollection services) {/ / register configuration to the service container services.Configure (Configuration.GetSection ("subsection")); / / var subsectionOptions = Configuration.GetSection ("subsection") .Get (); / / services.Configure (options = > / {/ / options.SubOption1 = subsectionOptions ["suboption1"]; / / options.SubOption2 = subsectionOptions ["suboption2"]; / /}) } public class HomeController: Controller {private TestSubSectionConfig _ subSectionConfig; private ILogger _ logger; public HomeController (IOptions option, ILogger logger) {_ subSectionConfig = option.Value; _ logger = logger;} public IActionResult Index () {_ logger.LogInformation ($"SubOption1: {_ subSectionConfig.SubOption1}"); _ logger.LogInformation ($"SubOption2: {_ subSectionConfig.SubOption2}") Return View ();}} Thank you for your reading. The above is the content of "what is the configuration in ASP.NET Core". After the study of this article, I believe you have a deeper understanding of what the configuration in ASP.NET Core has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.