Reputation: 6262
I am not sure what am I missing here, but I am not able to get the values from my appsettings.json in my .NET Core application. I have my appsettings.json as:
{
"AppSettings": {
"Version": "One"
}
}
Startup:
public class Startup
{
private IConfigurationRoot _configuration;
public Startup(IHostingEnvironment env)
{
_configuration = new ConfigurationBuilder()
}
public void ConfigureServices(IServiceCollection services)
{
//Here I setup to read appsettings
services.Configure<AppSettings>(_configuration.GetSection("AppSettings"));
}
}
Model:
public class AppSettings
{
public string Version{ get; set; }
}
Controller:
public class HomeController : Controller
{
private readonly AppSettings _mySettings;
public HomeController(IOptions<AppSettings> settings)
{
//This is always null
_mySettings = settings.Value;
}
}
_mySettings
is always null. Is there something that I am missing here?
Upvotes: 363
Views: 789055
Reputation: 3736
I guess the simplest way is by DI. An example of reaching into Controller.
// StartUp.cs
public void ConfigureServices(IServiceCollection services)
{
...
// For getting appsettings from anywhere
services.AddSingleton(Configuration);
}
public class ContactUsController : Controller
{
readonly IConfiguration _configuration;
public ContactUsController(
IConfiguration configuration)
{
_configuration = configuration;
// Sample:
var apiKey = _configuration.GetValue<string>("SendGrid:CAAO");
...
}
}
Upvotes: 49
Reputation: 1667
Just create an AnyName.cs file and paste the following code.
using System;
using System.IO;
using Microsoft.Extensions.Configuration;
namespace Custom
{
static class ConfigurationManager
{
public static IConfiguration AppSetting { get; }
static ConfigurationManager()
{
AppSetting = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("YouAppSettingFile.json")
.Build();
}
}
}
Must replace YouAppSettingFile.json file name with your file name.
Your .json file should look like below.
{
"GrandParent_Key" : {
"Parent_Key" : {
"Child_Key" : "value1"
}
},
"Parent_Key" : {
"Child_Key" : "value2"
},
"Child_Key" : "value3"
}
Now you can use it.
Don't forget to Add Reference in your class where you want to use.
using Custom;
Code to retrieve the value.
string value1 = ConfigurationManager.AppSetting["GrandParent_Key:Parent_Key:Child_Key"];
string value2 = ConfigurationManager.AppSetting["Parent_Key:Child_Key"];
string value3 = ConfigurationManager.AppSetting["Child_Key"];
Upvotes: 85
Reputation: 21536
ASP.NET Core 6.x brings another big changes in the Program
class:
Program.Main()
boilerplate if you select to use Top level statementsStartup
class as everything is in Program
fileWebApplication
and WebApplicationBuilder
Some said these changes are good for new comers to learn ASP.NET Core. I kind of felt the other way around. I think the separation of Program
and Startup
made more sense, at least to me.
Anyway...
This is how Program.cs
would look like:
// Program.cs
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/errors");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "areaRoute",
pattern: "{area:exists}/{controller=home}/{action=index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=home}/{action=index}/{id?}");
app.Run();
}
}
You can kind of tell the section between WebApplication.CreateBuilder()
and builder.Build()
is what the old ConfigureServices(IServiceCollection services)
used to do. And the section before app.Run()
is what the old Configure()
from Startup
used to do.
Most importantly, IConfiguration
is injected to the pipeline so you can use it on your controllers.
ASP.NET Core 3.x brings some changes that try to support other approaches such as worker services, so it replaces web host with a more generic host builder:
// Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
};
}
}
The Startup
class looks pretty similar to the 2.x version though.
You don't need to new IConfiguration
in the Startup
constructor. Its implementation will be injected by the DI system.
// Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
// Startup.cs
public class Startup
{
public IHostingEnvironment HostingEnvironment { get; private set; }
public IConfiguration Configuration { get; private set; }
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
this.HostingEnvironment = env;
this.Configuration = configuration;
}
}
You need to tell Startup
to load the appsettings files.
// Program.cs
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
}
//Startup.cs
public class Startup
{
public IConfigurationRoot Configuration { get; private set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
this.Configuration = builder.Build();
}
...
}
There are many ways you can get the value you configure from the app settings:
ConfigurationBuilder.GetValue<T>
Let's say your appsettings.json
looks like this:
{
"ConnectionStrings": {
...
},
"AppIdentitySettings": {
"User": {
"RequireUniqueEmail": true
},
"Password": {
"RequiredLength": 6,
"RequireLowercase": true,
"RequireUppercase": true,
"RequireDigit": true,
"RequireNonAlphanumeric": true
},
"Lockout": {
"AllowedForNewUsers": true,
"DefaultLockoutTimeSpanInMins": 30,
"MaxFailedAccessAttempts": 5
}
},
"Recaptcha": {
...
},
...
}
You can inject the whole configuration into the constructor of your controller/class (via IConfiguration
) and get the value you want with a specified key:
public class AccountController : Controller
{
private readonly IConfiguration _config;
public AccountController(IConfiguration config)
{
_config = config;
}
[AllowAnonymous]
public IActionResult ResetPassword(int userId, string code)
{
var vm = new ResetPasswordViewModel
{
PasswordRequiredLength = _config.GetValue<int>(
"AppIdentitySettings:Password:RequiredLength"),
RequireUppercase = _config.GetValue<bool>(
"AppIdentitySettings:Password:RequireUppercase")
};
return View(vm);
}
}
The ConfigurationBuilder.GetValue<T>
works great if you only need one or two values from the app settings. But if you want to get multiple values from the app settings, or you don't want to hard code those key strings in multiple places, it might be easier to use Options Pattern. The options pattern uses classes to represent the hierarchy/structure.
To use options pattern:
IOptions<T>
into the constructor of the controller/class you want to get values onYou can define classes with properties that need to exactly match the keys in your app settings. The name of the class does't have to match the name of the section in the app settings:
public class AppIdentitySettings
{
public UserSettings User { get; set; }
public PasswordSettings Password { get; set; }
public LockoutSettings Lockout { get; set; }
}
public class UserSettings
{
public bool RequireUniqueEmail { get; set; }
}
public class PasswordSettings
{
public int RequiredLength { get; set; }
public bool RequireLowercase { get; set; }
public bool RequireUppercase { get; set; }
public bool RequireDigit { get; set; }
public bool RequireNonAlphanumeric { get; set; }
}
public class LockoutSettings
{
public bool AllowedForNewUsers { get; set; }
public int DefaultLockoutTimeSpanInMins { get; set; }
public int MaxFailedAccessAttempts { get; set; }
}
And then you need to register this configuration instance in ConfigureServices()
in the start up:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
...
namespace DL.SO.UI.Web
{
public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
...
var identitySettingsSection =
_configuration.GetSection("AppIdentitySettings");
services.Configure<AppIdentitySettings>(identitySettingsSection);
...
}
...
}
}
Note: if you're not seeing the overload extension method of service.Configure<>
that takes IConfiguration
as parameter, you probably need to install Microsoft.Extensions.Options.ConfigurationExtensions
NuGet package.
Due to the changes I mentioned at the beginning for ASP.NET Core 6.x, you would need to bind the section and add it to the DI in the following:
// Program.cs
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.Configure<AppIdentitySettings>(
builder.Configuration.GetSection("AppIdentitySettings")
);
var app = builder.Build();
...
app.Run();
}
}
You can read more about this here.
Lastly on the controller/class you want to get the values, you need to inject IOptions<AppIdentitySettings>
through constructor:
public class AccountController : Controller
{
private readonly AppIdentitySettings _appIdentitySettings;
public AccountController(IOptions<AppIdentitySettings> appIdentitySettingsAccessor)
{
_appIdentitySettings = appIdentitySettingsAccessor.Value;
}
[AllowAnonymous]
public IActionResult ResetPassword(int userId, string code)
{
var vm = new ResetPasswordViewModel
{
PasswordRequiredLength = _appIdentitySettings.Password.RequiredLength,
RequireUppercase = _appIdentitySettings.Password.RequireUppercase
};
return View(vm);
}
}
Upvotes: 496
Reputation: 111
Add this in your startup. This will validate config while booting.
services.AddOptions<YourType>().BindConfiguration("YourAppSettingSectionName").ValidateDataAnnotations().ValidateOnStart();
Then inject IOptions in your constructer where you want to access the properties.
public Ctor(IOptions<YourType> options)
{
//Your code goes here
}
Note: The config property name should match with class property names else you'll have to map it manually.
Upvotes: 0
Reputation: 9
You can refer to this link if the above solutions are not help. For me the Named options support using IConfigureNamedOptions works! You may also find out other methods might be helpful for you.
Named options support using IConfigureNamedOptions
Upvotes: 0
Reputation: 199
here is an abstraction over .NET Framework and Core: web.config, app.config and appsettings.json
static SafeDictionary<string, string> _appSettings;
public static SafeDictionary<string, string> AppSettings {
get {
if (_appSettings == null)
{
_appSettings = ConfigurationManager.AppSettings
.ToDictionary()
.ToSafe();
BuildAppSettings( JsonAppSettings, "");
}
return _appSettings;
}
}
static SafeDictionary<string, string> _connectionStrings;
public static SafeDictionary<string, string> ConnectionStrings
{
get
{
if (_connectionStrings == null)
{
_connectionStrings = ConfigurationManager.ConnectionStrings
.Cast<ConnectionStringSettings>()
.ToDictionary(x => x.Name, x => x.ConnectionString)
.ToSafe();
foreach (var jp in JsonAppSettings["ConnectionStrings"].Cast<JProperty>())
_connectionStrings.Add(jp.Name, jp.Value.ToString() );
}
return _connectionStrings;
}
}
https://github.com/bitministry/common
Upvotes: 0
Reputation: 470
Suppose you have values like this in appsettings.json.
"MyValues": {
"Value1": "Xyz"
}
Method 1: Without Dependency Injection
In .cs file:
static IConfiguration conf = (new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json").Build());
public static string myValue1= conf["MyValues:Value1"].ToString();
Method 2: With Dependency Injection (Recommended)
In Startup.cs file:
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
...
services.AddServices(Configuration);
}
In your Controller:
public class TestController : ControllerBase
{
private string myValue1 { get; set; }
public TestController(IConfiguration configuration)
{
this.myValue1 = configuration.GetValue<string>("MyValues:Value1");
}
}
Upvotes: 24
Reputation: 319
Spent an hour trying to fix the same problem, my solution was to add PreserveNewest/CopyAlways for appconfig.json in csproj
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Upvotes: 0
Reputation: 1500
Adding to Abishek's answer:
If you want to import values into a static class, then just use (recommended by ReSharper):
static IConfiguration conf = (JsonConfigurationExtensions.AddJsonFile(new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()), "appsettings.json").Build());
private static string AuthorizationServiceURL { get; } = conf["ServiceUrls:AuthorizationUrl"];
// appsettings.json
{
"ServiceUrls": {
"AuthorizationUrl": "https://example.com/authorize"
}
}
Upvotes: 0
Reputation: 671
I just create a static class and set a config variable to it in Startup.cs
public static class GlobalConfig {
public static IConfiguration config { get; set; }
}
public class Startup
{
public Startup(IConfiguration configuration)
{
GlobalConfig.config = configuration;
}
}
Then use it anywhere:
var keyVal = GlobalConfig.config["key"];
Seems like the easiest way to access the config file and make it available anywhere.
Upvotes: 0
Reputation: 11
{
"Settings": {
"ProjectName": "Sample Project"
}
}
public class Settings
{
public string ProjectName { get; set; }
}
Startup.cs
:public void ConfigureServices(IServiceCollection services)
{
services.Configure<Settings>(Configuration.GetSection("Settings"));
}
public class TestController : Controller
{
private readonly Settings _settings;
public TestController(IOptions<Settings> settings)
{
_settings = settings.Value;
}
[AllowAnonymous]
public async Task<IActionResult> test()
{
var _projectname = _settings.ProjectName;
return View();
}
}
Upvotes: 1
Reputation: 101
I had a similar problem in WPF (.NET Framework 5.0)
All I had to do was to register it.
services.AddSingleton<IConfiguration>(_configuration);
The config itself was configured like this (in App.xaml.cs):
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
_configuration = builder.Build();
Upvotes: 2
Reputation: 715
In my case, I created everything from scratch and the appsettings.json
too, was not loaded at all. After some debugging, I discovered that the file was never copied to the "target folder".
To fix it, I simply had to set the correct file properties.
It looks like this:
Upvotes: 0
Reputation: 69
From Asp.net core 2.2 to above you can code as below:
Step 1. Create an AppSettings class file.
This file contains some methods to help get value by key from the appsettings.json file. Look like as code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ReadConfig.Bsl
{
public class AppSettings
{
private static AppSettings _instance;
private static readonly object ObjLocked = new object();
private IConfiguration _configuration;
protected AppSettings()
{
}
public void SetConfiguration(IConfiguration configuration)
{
_configuration = configuration;
}
public static AppSettings Instance
{
get
{
if (null == _instance)
{
lock (ObjLocked)
{
if (null == _instance)
_instance = new AppSettings();
}
}
return _instance;
}
}
public string GetConnection(string key, string defaultValue = "")
{
try
{
return _configuration.GetConnectionString(key);
}
catch
{
return defaultValue;
}
}
public T Get<T>(string key = null)
{
if (string.IsNullOrWhiteSpace(key))
return _configuration.Get<T>();
else
return _configuration.GetSection(key).Get<T>();
}
public T Get<T>(string key, T defaultValue)
{
if (_configuration.GetSection(key) == null)
return defaultValue;
if (string.IsNullOrWhiteSpace(key))
return _configuration.Get<T>();
else
return _configuration.GetSection(key).Get<T>();
}
public static T GetObject<T>(string key = null)
{
if (string.IsNullOrWhiteSpace(key))
return Instance._configuration.Get<T>();
else
{
var section = Instance._configuration.GetSection(key);
return section.Get<T>();
}
}
public static T GetObject<T>(string key, T defaultValue)
{
if (Instance._configuration.GetSection(key) == null)
return defaultValue;
if (string.IsNullOrWhiteSpace(key))
return Instance._configuration.Get<T>();
else
return Instance._configuration.GetSection(key).Get<T>();
}
}
}
Step 2. Initial configuration for AppSettings object
We need to declare and load appsettings.json file when the application starts, and load configuration information for AppSettings object. We will do this work in the constructor of the Startup.cs file.
Please notice line AppSettings.Instance.SetConfiguration(Configuration);
public Startup(IHostingEnvironment evm)
{
var builder = new ConfigurationBuilder()
.SetBasePath(evm.ContentRootPath)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{evm.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build(); // load all file config to Configuration property
AppSettings.Instance.SetConfiguration(Configuration);
}
Okay, now I have an appsettings.json file with some keys as below:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"ConnectionString": "Data Source=localhost;Initial Catalog=ReadConfig;Persist Security Info=True;User ID=sa;Password=12345;"
},
"MailConfig": {
"Servers": {
"MailGun": {
"Pass": "65-1B-C9-B9-27-00",
"Port": "587",
"Host": "smtp.gmail.com"
}
},
"Sender": {
"Email": "[email protected]",
"Pass": "123456"
}
}
}
Step 3. Read config value from an action
I make demo an action in Home controller as below :
public class HomeController : Controller
{
public IActionResult Index()
{
var connectionString = AppSettings.Instance.GetConnection("ConnectionString");
var emailSender = AppSettings.Instance.Get<string>("MailConfig:Sender:Email");
var emailHost = AppSettings.Instance.Get<string>("MailConfig:Servers:MailGun:Host");
string returnText = " 1. Connection String \n";
returnText += " " +connectionString;
returnText += "\n 2. Email info";
returnText += "\n Sender : " + emailSender;
returnText += "\n Host : " + emailHost;
return Content(returnText);
}
}
And below is the result:
Upvotes: 4
Reputation: 647
I found it easiest to do it the following with .NET Core 3+. I found all the other methods of using HostBuilders etc to be a bit longwinded and not as readable. This is not specifically for ASP.Net but you can adapt it...
There is a working example here: https://github.com/NotoriousPyro/PyroNexusTradingAlertBot/blob/develop/PyroNexusTradingAlertBot/Program.cs
Create the json:
{
"GlobalConfig": {
"BlacklistedPairs": [ "USD", "USDT", "BUSD", "TUSD", "USDC", "DAI", "USDK" ]
},
"CoinTrackingConfig": {
"Cookie1": "",
"Cookie2": "",
"ApiKey": "",
"ApiSecret": "",
"UpdateJobs": [
{
"Name": "Binance",
"Path": "binance_api",
"JobId": 42202
},
{
"Name": "Bitfinex",
"Path": "bitfinex_api",
"JobId": 9708
}
]
},
"DiscordConfig": {
"BotToken": ""
}
}
Create the class for the json objects:
class GlobalConfig
{
public string[] BlacklistedPairs { get; set; }
}
class CoinTrackingConfig
{
public string Cookie1 { get; set; }
public string Cookie2 { get; set; }
public string ApiKey { get; set; }
public string ApiSecret { get; set; }
public List<CoinTrackingUpdateJobs> UpdateJobs { get; set; }
}
class CoinTrackingUpdateJobs
{
public string Name { get; set; }
public string Path { get; set; }
public int JobId { get; set; }
}
class DiscordConfig
{
public string BotToken { get; set; }
}
Create a helper class:
private class Config
{
private IConfigurationRoot _configuration;
public Config(string config) => _configuration = new ConfigurationBuilder()
.AddJsonFile(config)
.Build();
public T Get<T>() where T : new()
{
var obj = new T();
_configuration.GetSection(typeof(T).Name).Bind(obj);
return obj;
}
}
The service provider options and service constructor:
public class DiscordServiceOptions
{
public string BotToken { get; set; }
}
public DiscordService(IOptions<DiscordServiceOptions> options, ILogger<DiscordService> logger)
{
_logger = logger;
_client = new DiscordSocketClient();
_client.Log += Log;
_client.Ready += OnReady;
_client.Disconnected += OnDisconnected;
_client.LoginAsync(TokenType.Bot, options.Value.BotToken);
_client.StartAsync();
}
Initialise it like so (Pass in the config into the service provider - the IOptions will be passed in when the service is built):
static async Task Main(string[] args)
{
var _config = new Config("config.json");
var globalConfig = config.Get<GlobalConfig>();
var coinTrackingConfig = config.Get<CoinTrackingConfig>();
var discordConfig = config.Get<DiscordConfig>();
_services = new ServiceCollection()
.AddOptions()
.Configure<DiscordServiceOptions>(options =>
{
options.BotToken = discordConfig.BotToken;
})
.AddSingleton<IDiscordService, DiscordService>()
.AddLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Trace);
logging.AddNLog(new NLogProviderOptions
{
CaptureMessageTemplates = true,
CaptureMessageProperties = true
});
})
.BuildServiceProvider();
}
Upvotes: 1
Reputation: 15345
This tends to happen particularly with vscode as I'd assume because of how one configures the launch.json
.
Based on this answer I had to reconfigure the base path the configuration is being searched for to that of DLL's path, and since the default setting was optional it was hard to track this down on a .net core 3.1 & net 5.0 app. Here's how I've reconfigured
Program.cs
:
using System;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace API
{
public class Program
{
public static int Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
return 0;
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(c =>
{
var codeBase = Assembly.GetExecutingAssembly().Location;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
var assembyDirectory = Path.GetDirectoryName(path);
c.SetBasePath(assembyDirectory);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
;
}
}
}
I could access the configuration fine in Startup.cs
:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Model;
namespace API
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var myOptions = Configuration.To<ApiConfig>();
services.AddAuthentication(myOptions.Secret);
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
}
Upvotes: 1
Reputation: 196
In startup.cs, add the constructor
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
Access the settings using Configuration["SecureCookies"]
Upvotes: 3
Reputation: 67
.NET core 3.X
no need to create new model and to set in Startup.cs.
Controller Add new package - using Microsoft.Extensions.Configuration;
public class HomeController : Controller
{
private readonly IConfiguration _mySettings;
public HomeController (IConfiguration mySettings)
{
_mySettings= mySettings;
}
//ex: you can get value on below function
public IEnumerable<string> Get()
{
var result = _config.GetValue<string>("AppSettings:Version"); // "One"
return new string[] { result.ToString() };
}
}
Upvotes: 5
Reputation: 70186
For ASP.NET Core 3.1 you can follow this guide:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1
When you create a new ASP.NET Core 3.1 project you will have the following configuration line in Program.cs
:
Host.CreateDefaultBuilder(args)
This enables the following:
This means you can inject IConfiguration
and fetch values with a string key, even nested values. Like IConfiguration["Parent:Child"];
Example:
appsettings.json
{
"ApplicationInsights":
{
"Instrumentationkey":"putrealikeyhere"
}
}
WeatherForecast.cs
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
private readonly IConfiguration _configuration;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var key = _configuration["ApplicationInsights:InstrumentationKey"];
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
Upvotes: 5
Reputation: 155
I think the best option is:
Create a model class as config schema
Register in DI: services.Configure(Configuration.GetSection("democonfig"));
Get the values as model object from DI in your controller:
private readonly your_model myConfig;
public DemoController(IOptions<your_model> configOps)
{
this.myConfig = configOps.Value;
}
Upvotes: 0
Reputation: 74
In my case it was simple as using the Bind() method on the Configuration object. And then add the object as singleton in the DI.
var instructionSettings = new InstructionSettings();
Configuration.Bind("InstructionSettings", instructionSettings);
services.AddSingleton(typeof(IInstructionSettings), (serviceProvider) => instructionSettings);
The Instruction object can be as complex as you want.
{
"InstructionSettings": {
"Header": "uat_TEST",
"SVSCode": "FICA",
"CallBackUrl": "https://UATEnviro.companyName.co.za/suite/webapi/receiveCallback",
"Username": "s_integrat",
"Password": "X@nkmail6",
"Defaults": {
"Language": "ENG",
"ContactDetails":{
"StreetNumber": "9",
"StreetName": "Nano Drive",
"City": "Johannesburg",
"Suburb": "Sandton",
"Province": "Gauteng",
"PostCode": "2196",
"Email": "[email protected]",
"CellNumber": "0833 468 378",
"HomeNumber": "0833 468 378",
}
"CountryOfBirth": "710"
}
}
Upvotes: 4
Reputation: 139
public static void GetSection()
{
Configuration = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.Build();
string BConfig = Configuration.GetSection("ConnectionStrings")["BConnection"];
}
Upvotes: 13
Reputation: 13298
In the constructor of Startup class, you can access appsettings.json and many other settings using the injected IConfiguration object:
Startup.cs Constructor
public Startup(IConfiguration configuration)
{
Configuration = configuration;
//here you go
var myvalue = Configuration["Grandfather:Father:Child"];
}
public IConfiguration Configuration { get; }
Contents of appsettings.json
{
"Grandfather": {
"Father": {
"Child": "myvalue"
}
}
Upvotes: 19
Reputation: 22597
Adding to David Liang's answer for Core 2.0 -
appsettings.json
file's are linked to ASPNETCORE_ENVIRONMENT
variable.
ASPNETCORE_ENVIRONMENT
can be set to any value, but three values are supported by the framework: Development
, Staging
, and Production
. If ASPNETCORE_ENVIRONMENT
isn't set, it will default to Production
.
For these three values these appsettings.ASPNETCORE_ENVIRONMENT.json files are supported out of the box - appsettings.Staging.json
, appsettings.Development.json
and appsettings.Production.json
The above three application setting json files can be used to configure multiple environments.
Example - appsettings.Staging.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"System": "Information",
"Microsoft": "Information"
}
},
"MyConfig": "My Config Value for staging."
}
Use Configuration["config_var"]
to retrieve any configuration value.
public class Startup
{
public Startup(IHostingEnvironment env, IConfiguration config)
{
Environment = env;
Configuration = config;
var myconfig = Configuration["MyConfig"];
}
public IConfiguration Configuration { get; }
public IHostingEnvironment Environment { get; }
}
Upvotes: 62