Add a json
file to your project root dir: config.json
{
"AppSettings": {
"TestKey" : "TestValue"
}
}
Create a new class
for config deserialization:
public class AppSettings
{
public string TestKey { get; set; }
}
In Startup.cs
:
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Setup configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
var builder = services.AddMvc();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
Get the options in your controller
:
public HomeController(IOptions<AppSettings> settings)
{
var value = settings.Value.TestKey;
}