Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Configuration in ASP.NET Core

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to configure Configuration in ASP.NET Core". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to configure Configuration in ASP.NET Core.

1. Preface

ASP.NET Core introduces Microsoft.Extensions.Configuration configuration into the application, which can support a variety of configurations, including command line configuration, environment variable configuration, file configuration, memory configuration, custom configuration, and so on. Let's talk about some of these configurations.

two。 Command line configuration

CommandLineConfigurationProvider loads the configuration from (for example, DOS) command line parameter key values when the application is running. To activate the command line configuration, call the AddCommandLine extension method on the instance of ConfigurationBuilder. AddCommandLine is automatically called when a new WebHostBuilder is initialized with CreateDefaultBuilder.

Public class Program {public static void Main (string [] args) {CreateWebHostBuilder (args). Build (). Run ();} public static IWebHostBuilder CreateWebHostBuilder (string [] args) = > WebHost.CreateDefaultBuilder (args) .ConfigreAppConfiguration ((hostingContext, config) = > {/ / Call other providers here and call AddCommandLine last. Config.AddCommandLine (args);}. UseStartup ();}

After adding the above code to Program and adding the following code to the / Home/Index view, release a Web version and mount it on IIS.

Enter the DOS command line dotnet D:\ Release\ Core\ TestWebApp.dll CommandLineKey1=value1 configuration key: CommandLineKey1, value: value1 on the console, and you will see the following interface information:

According to the console, listen to the site connection, open one of them in the browser, such as https://localhost:5001/, and you will see our configuration command line information:

3. File configuration

FileConfigurationProvider is the base class for loading configurations from the file system. The following configurations provide specific file types for applications: INI configuration, JSON configuration, XML configuration.

3.1 INI configuration

IniConfigurationProvider loads the configuration from the INI file key-value pair at run time. To activate the INI file configuration, call the AddIniFile extension method on the instance of ConfigurationBuilder, and the colon can be used as a node delimiter in the INI file configuration. Now let's add an INI configuration file (named config) under the CoreWeb root:

[section0] key0=valuekey1= value[section1] subsection:key= value[section2: subsection0] key= value[section2: subsection1] key=value

The application calls ConfigureAppConfiguration when building the host to specify the application configuration (here we specify the config.ini file):

Public class Program {public static void Main (string [] args) {CreateWebHostBuilder (args). Build (). Run ();} public static IWebHostBuilder CreateWebHostBuilder (string [] args) = > WebHost.CreateDefaultBuilder (args) .ConfigreAppConfiguration ((hostingContext, config) = > {config.SetBasePath (Directory.GetCurrentDirectory () Config.AddIniFile (config.ini, optional: true, reloadOnChange: true);}) .UseStartup ();}

From the above code, you can see that IConfigurationBuilder sets the initial path for file access. Optional: whether the file is optional; reloadOnChange: whether the configuration should be reloaded if the file changes. When you start the application, you will see the following configuration information:

3.2 JSON configuration

JsonConfigurationProvider loads the configuration from the JSON file key-value pair during runtime. To activate the JSON file configuration, call the AddJsonFile extension method on the instance of ConfigurationBuilder. When you initialize a new WebHostBuilder with CreateDefaultBuilder, AddJsonFile is automatically called twice, and the method (AddJsonFile) is called to load the configuration from the following file, which is read first by appsettings.json. When the application starts, the corresponding appsettings. {Environment} .json environment version is called by default. For example, appsettings. {Environment} .json loads the environment version of the corresponding file (development mode, production mode, etc.) according to IHostingEnvironment.EnvironmentName. Now let's add a JSON configuration file (named config) under the CoreWeb root:

{section0: {"key0": "key0value", "key1": "key1value"}, "section1": {"key0": "key0value", "key1": "key1value"}, "section2": {"subsection0": {"key0": "sub0key0value", "key1": "sub0key1value"}, "subsection1": {"key0": "sub1key0value" Key1: "sub1key1value"}

The application calls ConfigureAppConfiguration when building the host to specify the application configuration for files other than appsettings.json and appsettings. {Environment} .json (here we specify the config.json file):

Public class Program {public static void Main (string [] args) {CreateWebHostBuilder (args). Build (). Run ();} public static IWebHostBuilder CreateWebHostBuilder (string [] args) = > WebHost.CreateDefaultBuilder (args) .ConfigreAppConfiguration ((hostingContext, config) = > {config.SetBasePath (Directory.GetCurrentDirectory () Config.AddJsonFile (config.json, optional: true, reloadOnChange: true);}) .UseStartup ();}

When you start the application, you will see the following configuration information:

As you can see from the above information, after we specify the config.json file, IConfigurationBuilder will call AddJsonFile one more time, plus the first two AddJsonFile, for a total of three times.

3.2.1GetSection, GetChildren and Exists

(1) GetSection:IConfiguration.GetSection gets the specified configuration section. Let's learn about it through an example:

Public Startup (IConfiguration configuration) {Configuration = configuration; var configSection0 = Configuration.GetSection ("section0"); var configSection1 = Configuration.GetSection ("section0:key0");}

When you start the application, you will see the following configuration information:

As you can see from the above example, IConfiguration.GetSection only gets the configSection0 node key and path in the json data, but not its node value. To get the key value in section0:key0, provide the full node path when calling GetSection, such as the example of getting the configSection1 key value.

(2) GetChildren: get the specified configuration tree node. Let's learn about it through an example:

Public Startup (IConfiguration configuration) {Configuration = configuration; var configSection = Configuration.GetSection ("section2"); var children = configSection.GetChildren ();

When you start the application, you will see the following configuration information:

GetChildren gets all the nodes under the specified section2 node.

(3) Exists: use ConfigurationExtensions.Exists to determine whether the configuration node exists. Let's learn about it through an example:

Public Startup (IConfiguration configuration) {Configuration = configuration; var sectionExists0 = Configuration.GetSection ("section2") .Exists (); / / true var sectionExists1 = Configuration.GetSection ("section2:subsection2") .Exists (); / / false}

When you start the application, you will learn that the section2 configuration node will return true if it exists, and vice versa, false; is the same as the section2:subsection2 path configuration node.

3.3 XML configuration

XmlConfigurationProvider loads the configuration from the XML file key-value pair at run time. To activate the XML file configuration, call the AddXmlFile extension method on the instance of ConfigurationBuilder. Now let's add a XML configuration file (named config) under the CoreWeb root:

Value

The application calls ConfigureAppConfiguration when building the host to specify the configuration of the application (here we specify the config.xml file):

Public class Program {public static void Main (string [] args) {CreateWebHostBuilder (args). Build (). Run ();} public static IWebHostBuilder CreateWebHostBuilder (string [] args) = > WebHost.CreateDefaultBuilder (args) .ConfigreAppConfiguration ((hostingContext, config) = > {config.SetBasePath (Directory.GetCurrentDirectory () Config.AddXmlFile (config.xml, optional: true, reloadOnChange: true);}) .UseStartup ();}

When you start the application, you will see the following configuration information:

Thank you for reading, the above is the content of "how to configure Configuration in ASP.NET Core". After the study of this article, I believe you have a deeper understanding of how to configure Configuration in ASP.NET Core, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report