ASP.NET Core class library fetch profile appsettings.json

ASP.NET Core class library fetch profile appsettings.json

First reference NuGet package

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

 

Let's look appsettings.json file

 
  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. }  

We would like to take ProviderName how to do it? First, a new ConfigManager

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

GetAppsettings way

 
  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.  }  

Examples of call

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

 

Source: https://www.studenty.cn/?p=1094

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

We need to quote the official nuget package

①:Microsoft.Extensions.Configuration

②:Microsoft.Extensions.Options.ConfigurationExtensions

User-defined profiles json

Here profile name I use appsettings.json

Content profile as shown:

In the Startup method Startup class in the code editor, I put the code posted Come:

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);



In the code ConnectionStrings class is a Model, and then you create connString variable has been instantiated. You can visit


Author: Oscar skin
link: https: //www.jianshu.com/p/a13a0194ff91
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/mq0036/p/10957010.html