.net core 读取配置文件的几种方式

2023-10-31 13:21:39 浏览数 (1)

一、Json配置文件

1、这里的配置文件指的是下图

2、json配置文件示例

代码语言:c#复制
{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "Setting": {
        "Url": "http://localhost:8080/",
        "Name": "localhost"
    }
}

二、读取配置文件的几种方式

1、方式一:直接读取

代码语言:c#复制
[ApiController]
[Route("[controller]/[action]")]
public class TestController: ControllerBase
{
    public IConfiguration _configuration { get; set; }
    public TestController(IConfiguration configuration)
    {
        _configuration = configuration;
    }
    /// <summary>
    /// 方式一:直接读取单个值
    /// </summary>
    [HttpGet, HttpPost]
    public void GetConfigDemo1()
    {
        var url = _configuration["Setting:Url"]; // http://localhost:8080/
        var url2 = _configuration.GetValue<string>("Setting:Url"); // http://localhost:8080/
        var url3 = _configuration.GetSection("Setting").GetSection("Url").Value; // http://localhost:8080/
    }
}

2、方式二:读取Json对象

1)新建应用设置类AppSettings

代码语言:c#复制
/// <summary>
/// 应用设置类
/// 总类:对应json文件,确定json模块与对象
/// </summary>
public static class AppSettings
{
    public static SettingClass settingClass { get; set; }
    public static void Init(IConfiguration configuration)
    {
        // 将Setting模块绑定到Json模块的Setting类
        settingClass = new SettingClass();
        configuration.Bind("Setting", settingClass);
    }
}

2)新建Json模块Setting类

代码语言:c#复制
/// <summary>
/// Json模块Setting类
/// </summary>
public class SettingClass
{
    /// <summary>
    /// 地址
    /// </summary>
    public string Url { get; set; }
    /// <summary>
    /// 名称
    /// </summary>
    public string Name { get; set; }
}

3)在Startup.cs中调用AppSettings的初始化方法

代码语言:c#复制
// AppSettings总类进行调用静态方法
AppSettings.Init(Configuration);

4)在控制器中使用

代码语言:c#复制
[ApiController]
[Route("[controller]/[action]")]
public class TestController: ControllerBase
{
    public IConfiguration _configuration { get; set; }
    public TestController(IConfiguration configuration)
    {
        _configuration = configuration;
    }
    /// <summary>
    /// 方式二:读取Json对象 - 变成类使用
    /// </summary>
    [HttpGet, HttpPost]
    public void GetConfigDemo2()
    {
        var url = AppSettings.settingClass.Url; // http://localhost:8080/
        var name = AppSettings.settingClass.Name; // localhost
    }
}

3、方式三:在注册服务中绑定实体类与Json文件,使用时声明为全局常量

1)在Startup.cs中将Json模块类与Json文件对应内容绑定(Json模块类如方式2的SettingClass类)

代码语言:c#复制
services.Configure<SettingClass> (option =>
{
    option.Url = Configuration["Setting:Url"];
    option.Name = Configuration["Setting:Name"];
});

2)在控制器中使用

代码语言:c#复制
[ApiController]
[Route("[controller]/[action]")]
public class TestController: ControllerBase
{
    public IConfiguration _configuration { get; set; }
    public string UrlStr { get; set; }
    public string NameStr { get; set; }
    public TestController(IConfiguration configuration, IOptions<SettingClass> settings)
    {
        _configuration = configuration;
        UrlStr = settings.Value.Url;
        NameStr = settings.Value.Name;
    }
    /// <summary>
    /// 方法三:在注册服务的时候把配置文件与类绑定好值,使用时声明为全局常量
    /// </summary>
    [HttpGet, HttpPost]
    public void GetConfigDemo3()
    {
        var url = UrlStr; // http://localhost:8080/
        var name = NameStr; // localhost
    }
}

以上就是.net core 读取配置文件的几种方式的介绍,做此记录,如有帮助,欢迎点赞关注收藏!

0 人点赞