ASP.NET Core中的Configuration配置一

作者:暗断肠 时间:2024-06-05 09:32:59 

相关文章

ASP.NET Core2.2 中的Configuration配置一

ASP.NET Core2.2 中的Configuration配置二

1.前言

ASP.NET Core在应用程序上引入Microsoft.Extensions.Configuration配置,可以支持多种方式配置,包括命令行配置、环境变量配置、文件配置、内存配置,自定义配置等等。下面我们就其中几个配置来聊聊。

2.命令行配置

CommandLineConfigurationProvider在应用程序运行时会从(例如DOS)命令行参数键值加载配置。要激活命令行配置,请在ConfigurationBuilder的实例上调用AddCommandLine扩展方法。使用CreateDefaultBuilder初始化新的WebHostBuilder时会自动调用AddCommandLine。

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

在Program加入上述代码并在/Home/Index视图上添加如下代码后发布一个Web版本挂载在IIS上。

ASP.NET Core中的Configuration配置一

ASP.NET Core中的Configuration配置一

在控制台上输入DOS命令行dotnet D:\Release\Core\TestWebApp.dll CommandLineKey1=value1配置键:CommandLineKey1,值:value1的信息,会看到如下界面信息:

ASP.NET Core中的Configuration配置一

根据控制台监听站点连接,在浏览器上打开其中一个,比如打开https://localhost:5001/,会看到我们配置命令行信息:

ASP.NET Core中的Configuration配置一

3.文件配置

FileConfigurationProvider是从文件系统加载配置的基类。以下配置为应用程序提供专用于特定文件类型:INI配置、JSON配置、XML配置。

3.1 INI配置

IniConfigurationProvider在运行时会从INI文件键值对加载配置。若要激活INI文件配置,请在 ConfigurationBuilder的实例上调用AddIniFile扩展方法,而冒号可用作INI文件配置中的节点分隔符。现在我们在CoreWeb根目录下添加一个INI配置文件(命名为config):

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

而应用程序在构建主机时会调用ConfigureAppConfiguration以指定应用程序配置(这里我们指定config.ini文件):

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

从上述代码可以看到IConfigurationBuilder设置了文件访问初始路径。Optional:文件是否可选;reloadOnChange:如果文件更改,是否应重新加载配置。而通过启动应用程序时会看到如下配置信息:

ASP.NET Core中的Configuration配置一

3.2 JSON配置

JsonConfigurationProvider在运行时期间会从JSON文件键值对加载配置。若要激活JSON文件配置,请在ConfigurationBuilder的实例上调用AddJsonFile扩展方法。使用 CreateDefaultBuilder初始化新的WebHostBuilder时,会自动调用AddJsonFile两次,调用该方法(AddJsonFile)来从以下文件加载配置,首先会读取appsettings.json该文件。而应用程序启动时是会默认调用对应的appsettings.{Environment}.json环境版本的,例如appsettings.{Environment}.json会根据IHostingEnvironment.EnvironmentName加载对应文件的环境版本(开发模式、生产模式等)。现在我们在CoreWeb根目录下添加一个JSON配置文件(命名为config):

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

而应用程序在构建主机时会调用ConfigureAppConfiguration以指定除appsettings.json和appsettings.{Environment}.json以外文件的应用程序配置(这里我们指定config.json文件):

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

而通过启动应用程序时会看到如下配置信息:

ASP.NET Core中的Configuration配置一

从上述信息可以看到,在我们指定config.json文件后,IConfigurationBuilder会额外多调用一次AddJsonFile,加上前两次AddJsonFile,一共是三次。

3.2.1GetSection、GetChildren和Exists

(1)GetSection:IConfiguration.GetSection获取指定配置子节。下面我们通过一个示例来了解下:

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

而通过启动应用程序时会看到如下配置信息:

ASP.NET Core中的Configuration配置一

ASP.NET Core中的Configuration配置一

通过上述示例可以看到,IConfiguration.GetSection仅仅是获取到json数据里面configSection0节点键和路径,并没有获取到它的节点值。若要获取section0:key0中的键值,请在调用GetSection时提供完整节点路径,如获取configSection1键值示例。
(2)GetChildren:获取指定配置树节点。下面我们通过一个示例来了解下:

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

而通过启动应用程序时会看到如下配置信息:

ASP.NET Core中的Configuration配置一

ASP.NET Core中的Configuration配置一

GetChildren获取了指定section2节点下所有节点。
(3)Exists:使用ConfigurationExtensions.Exists确定配置节点是否存在。下面我们通过一个示例来了解下:

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

而通过启动应用程序时会了解到section2配置节点如果存在就会返回true,反之则false;同理section2:subsection2路径配置节点亦一样。

3.3 XML配置

XmlConfigurationProvider在运行时会从XML文件键值对加载配置。若要激活XML文件配置,请在ConfigurationBuilder的实例上调用AddXmlFile扩展方法。现在我们在CoreWeb根目录下添加一个XML配置文件(命名为config):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <section0>
   <key0>value</key0>
   <key1>value</key1>
 </section0>
 <section1>
   <key0>value</key0>
   <key1>value</key1>
 </section1>
</configuration>

而应用程序在构建主机时调用ConfigureAppConfiguration以指定应用程序的配置(这里我们指定config.xml文件):

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

而通过启动应用程序时会看到如下配置信息:

ASP.NET Core中的Configuration配置一

来源:https://www.cnblogs.com/wzk153/p/11263857.html

标签:ASP.NET,Core,Configuration,配置
0
投稿

猜你喜欢

  • Django中auth模块用户认证的使用

    2023-02-08 13:49:58
  • 深度剖析Golang中的数组,字符串和切片

    2024-02-17 08:53:13
  • vue权限路由实现的方法示例总结

    2024-06-05 15:29:41
  • 启动targetcli时遇到错误解决办法

    2023-03-20 05:59:43
  • golang正则之命名分组方式

    2024-05-22 10:17:10
  • Python实现将wav转amr,并转换成hex数组

    2023-06-29 08:18:59
  • Go每日一库之dateparse处理时间

    2024-04-26 17:25:55
  • Python如何获取多线程返回结果

    2024-01-01 23:34:28
  • 使用Python解决常见格式图像读取nii,dicom,mhd

    2021-11-14 23:36:59
  • Python操作MySQL简单实现方法

    2024-01-19 04:45:44
  • Pandas聚合运算和分组运算的实现示例

    2023-11-10 03:49:14
  • 微信小程序开发之获取用户手机号码(php接口解密)

    2023-11-15 03:34:59
  • 基于vue实现微博三方登录流程解析

    2024-05-03 15:08:42
  • 关于代码阅读问题的小技巧 脚本之家原创(适合所有网站)不定时更新

    2024-01-04 14:57:42
  • python opencv 简单阈值算法的实现

    2023-04-04 04:23:03
  • Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页

    2009-03-04 10:56:00
  • Idea 2022激活码最新汇总(亲测有效)

    2023-01-19 10:52:41
  • Python函数进阶之迭代器的原理与使用详解

    2023-03-28 09:02:01
  • Python多线程应用于自动化测试操作示例

    2021-04-09 19:20:58
  • Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】

    2021-09-16 17:17:44
  • asp之家 网络编程 m.aspxhome.com