如何使用ASP.NET Core 配置文件

2023-12-05 0 889
目录
  • 前言
  • Json配置文件的使用
    • RedisHelper类
  • XML配置文件的使用

    前言

    在ASP.NET ,我们使用XML格式的.Config文件来作为配置文件,而在ASP.NET Core,我们有了更多的选择,可以用回XML,也可以用Json、Ini文件作为配置文件

    Json配置文件的使用

    在创建ASP.NET Core的项目的时候,框架会自动添加appsettings.json文件和添加IConfiguration的注入。

    public Startup(IConfiguration configuration)
    {
    Configuration = configuration;
    }

    当我们在Startup构造函数添加一个IConfiguration参数,框架就会根据注入库来进行注入,除此之外还有IHostingEnvironment,如果在构造函数添加这个参数,框架也会注入对应的实现类

    如果我们想要自己添加Json配置,该怎么做呢?

    //SetBasePath方法用来指定配置文件的所在地,env.ContentRootPath是获取或设置包含应用程序内容文件的目录的绝对路径。
    //AddJsonFile方法是使用JsonConfigurationSource来接收Json文件,并添加到ConfigurationBuilder中的Sources中
    //Build()调用
    var config=new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
    .AddJsonFile(\”appsettings.json\”)
    .Build();
    Configuration = config;

    如果不通过IHostingEnvironment来获取绝对路径,也可以使用Directory.GetCurrentDirectory()方法来获得

    测试:

    public IActionResult Index()
    {
    var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile(\”appsettings.json\”).Build();
    string value = config.GetConnectionString(\”MySqlConnection\”);

    string value2 = config.GetSection(\”Test\”).Value;

    return Content($\”{value},Test:{value2}\”);
    }
    public IActionResult Index()
    {
    var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile(\”appsettings.json\”).Build();
    string value = config.GetConnectionString(\”MySqlConnection\”);

    string value2 = config.GetSection(\”Test\”).Value;

    return Content($\”{value},Test:{value2}\”);
    }

    如何使用ASP.NET Core 配置文件

    那复杂的键值或者数组,又该如何获得呢?

    {
    \”Teacher\”: {
    \”name\”: \”Tom\”,
    \”age\”: \”12\”,
    \”Students\”: [
    {
    \”name\”: \”Docker\”,
    \”age\”: \”13\”
    },
    {
    \”name\”: \”Nginx\”,
    \”age\”: \”45\”
    }
    ]
    }
    }

    我们想要获取Teacher的name值和数组Students第二个的name值,怎么获取呢?

    public IActionResult Index()
    {
    var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile(\”appsettings.json\”).Build();
    string value = config.GetSection(\”Teacher:name\”).Value;
    //
    string value2 = config.GetSection(\”Teacher:Students:1:name\”).Value;

    return Content($\”{value},Test:{value2}\”);

    }

    如何使用ASP.NET Core 配置文件

    PS:从Teacher:name和Teacher:Students:1:name这两个中可以寻找规律,当然获取方式不止这一种,还可以使用Config[“Teacher:Students:1:name”]来获取

    如果我们想用对象来存储配置文件的键值该如何做呢?

    //appsetting.json
    {
    \”RedisConfig\”: {
    \”host\”: \”127.0.0.1\”,
    \”MasterPort\”: \”6379\”,
    \”SlavePort\”: \”6380\”,
    \”PassWord\”: \”wen123\”
    }
    }

    RedisHelper类

    public class RedisHelper:IRedis
    {
    public string host { get; set; }

    public string MasterPort { get; set; }

    public string SlavePort { get; set; }

    public string PassWord { get; set; }

    }
    public IActionResult Index()
    {
    var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile(\”appsettings.json\”).Build();
    //创建一个自带的IOC容器
    var collection = new ServiceCollection();
    collection.AddOptions().Configure<RedisHelper>(config.GetSection(\”RedisConfig\”));
    RedisHelper redishelper = collection.BuildServiceProvider().GetService<IOptions<RedisHelper>>().Value;

    return Content($\”host:{redishelper.host},MasterPort:{redishelper.MasterPort}\”);
    }

    如何使用ASP.NET Core 配置文件

    还有另一种写法:在Startup类的ConfigureServices方法里面,向services添加代码,通过构造函数来构造RedisHelper类

    services.AddOptions().Configure<RedisHelper>(Configuration.GetSection(\”RedisConfig\”));
    private RedisHelper _redis;

    public HomeController(IOptions<RedisHelper> options)
    {
    _redis = options.Value;
    }

    public IActionResult Index()
    {
    return Content($\”host:{_redis.host},MasterPort:{_redis.MasterPort}\”);
    }

    如何使用ASP.NET Core 配置文件

    XML配置文件的使用

    这里简单记录一下,提取配置文件的值大致与上面做法没有太大的区别,在构造IConfiguration的时候把AddJsonFile改成AddXmlFile就行了

    //XMLDemo文件
    <?xml version=\”1.0\” encoding=\”utf-8\” ?>
    <Test>
    <mysqlConnectionStrings>sdfl</mysqlConnectionStrings>
    <test>
    <connection>sdfasdf</connection>
    <connection2>sdfdsafsfs</connection2>
    </test>
    <test2>
    <test3>
    <connection>dfgfdg</connection>
    </test3>
    </test2>
    </Test>
    public IActionResult Index()
    {
    var config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
    .AddXmlFile(\”XMLDemo.xml\”).Build();
    var value = config.GetSection(\”mysqlConnectionStrings\”).Value;
    var value2 = config.GetSection(\”test:connection2\”).Value;

    return Content($\”value:{value},value2:{value2}\”);

    如何使用ASP.NET Core 配置文件

    到此这篇关于如何使用ASP.NET Core 配置文件的文章就介绍到这了,更多相关ASP.NET Core 配置文件内容请搜索悠久资源网以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源网!

    您可能感兴趣的文章:

    • ASP.NETCore配置文件的获取和设置
    • ASP.NET CORE读取json格式配置文件
    • ASP.NETCore读取配置文件
    • ASP.NETCore应用程序配置文件AppSetting.json
    • ASP.NET Core根据环境变量支持多个 appsettings.json配置文件
    • ASP.NET Core中修改配置文件后自动加载新配置的方法详解
    • Asp.net Core与类库读取配置文件信息的方法
    • 如何在ASP.NET Core类库项目中读取配置文件详解
    • ASP.NET core Web中使用appsettings.json配置文件的方法
    • 详解ASP.NET Core 在 JSON 文件中配置依赖注入

    收藏 (0) 打赏

    感谢您的支持,我会继续努力的!

    打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
    点赞 (0)

    悠久资源 ASP.NET 如何使用ASP.NET Core 配置文件 https://www.u-9.cn/biancheng/aspnet/93936.html

    常见问题

    相关文章

    发表评论
    暂无评论
    官方客服团队

    为您解决烦忧 - 24小时在线 专业服务