zoukankan      html  css  js  c++  java
  • ASP.NET Core 类库中取读配置文件 appsettings.json

    ASP.NET Core 类库中取读配置文件 appsettings.json

    首先引用NuGet包

    1. Microsoft.Extensions.Configuration  
    2. Microsoft.Extensions.Configuration.Json  
    3. Microsoft.Extensions.DependencyInjection  
    4. Microsoft.Extensions.Options  
    5. Microsoft.Extensions.Options.ConfigurationExtensions  

    我们先来看一下appsettings.json文件

     
    1. {  
    2.   "Logging": {  
    3.     "IncludeScopes"false,  
    4.     "Debug": {  
    5.       "LogLevel": {  
    6.         "Default""Warning"  
    7.       }  
    8.     },  
    9.     "Console": {  
    10.       "LogLevel": {  
    11.         "Default""Warning"  
    12.       }  
    13.     }  
    14.   },  
    15.   "AppSupportDatabase": {  
    16.     "ConnectionString""server=.;initial catalog=TestDB;user id=sa;password=123",  
    17.     "ProviderName""System.Data.SqlClient"  
    18.   }  
    19. }  

    我们想取ProviderName怎么办呢?首先新建ConfigManager

     
    1. public class ConfigManager  
    2.   {  
    3.       public string ProviderName { getset; }  
    4.   
    5.       public string ConnectionString { getset; }  
    6.   }  

    GetAppsettings方法

     
    1. public T GetAppsettings<T>(string key) where T : classnew()  
    2.  {  
    3.      string keyDir = System.IO.Directory.GetCurrentDirectory();  
    4.   
    5.      IConfiguration config = new ConfigurationBuilder()  
    6.          .SetBasePath(keyDir)  
    7.          .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })  
    8.          .Build();  
    9.      var appconfig = new ServiceCollection()  
    10.          .AddOptions()  
    11.          .Configure<T>(config.GetSection(key))  
    12.          .BuildServiceProvider()  
    13.          .GetService<IOptions<T>>()  
    14.          .Value;  
    15.      return appconfig;  
    16.  }  

    调用例子

     
    1. GetAppsettings<ConfigManager>("AppSupportDatabase").ProviderName  

    出处:https://www.studenty.cn/?p=1094

    ==========================================

    需要先引用官方的nuget包

    ①:Microsoft.Extensions.Configuration

    ②:Microsoft.Extensions.Options.ConfigurationExtensions

    用户自定义json的配置文件

    在这里我用的配置文件名称是appsettings.json

    配置文件内容如图所示:

    在Startup类中的Startup方法中编辑代码,我先把代码贴出来吧:

    var builder = new ConfigurationBuilder()

    .SetBasePath(env.ContentRootPath)

    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

    .AddEnvironmentVariables();

    Configuration = builder.Build();

    var connString = new ConnectionStrings();

    Configuration.GetSection("ConnString").Bind(connString);



    在代码中ConnectionStrings类是一个Model,然后你创建的变量connString已经被实例化了。你可以访问了


    作者:奥斯卡的肌肤
    链接:https://www.jianshu.com/p/a13a0194ff91
    来源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    (9)ASP.NET Core2.2 中的MVC路由二
    (8)ASP.NET Core2.2 中的MVC路由一
    (7)ASP.NET Core2.2 中的错误处理
    (6)ASP.NET Core2.2 中使用IHttpClientFactory发出HTTP请求
    (5)ASP.NET Core2.2 中的静态文件
    (4)ASP.NET Core2.2 中间件
    蒲公英 &#183; JELLY技术周刊 Vol.05: Rust & Electron 的高性能实践 -- Finda
    Taro 2.2 全面插件化,支持拓展和定制个性化功能
    使用 Taro 快速开发京东小程序
    技术周刊 · 0202 年了,你还不学一下 WASM 么?
  • 原文地址:https://www.cnblogs.com/mq0036/p/10957010.html
Copyright © 2011-2022 走看看