I have some settings in my appSettings.json
file that i would like to retrieve. In order to do get values from the appSettings file I did the below
private IConfiguration configuration;
public class MyController
{
public MyController(IConfiguration configure)
{
configuration = configure;
}
}
In my method i then did something like
var someString = configuration.GetValue<string>("Smtp:Host");
This returned the value of the SMTP host form the config file which is what i wanted.
I then came across another Controller
that also required this same setting so instead of copying and pasting from one controller to another i decided to create a new static class in a class library.
I declared the class
public static class GlobalConfiguration
{
private static readonly IConfiguration config;
public static string Host {get;} = config.GetValue("Smtp:host");
}
Tried to use it and it threw an error (object not set to an instance).
How could i bring IConfiguration
into a static class so i can use this same setting across my app rather than having to paste it from one Controller to another?