I have appsettings.json
file where I want to declare paths to files.
"Paths": {
"file": "C:/file.pdf"
}
I want to access this value in my service, I try it to do like this:
public class ValueService: IValueService
{
IConfiguration Configuration { get; set; }
public MapsService(IConfiguration configuration)
{
this.Configuration = configuration;
}
public string generateFile()
{
var path = Configuration["Paths:file"] ;
}
}
however I get null values for var path
Startup.cs
file has appsettings.json
declared as it takes connection string from there. Is it possible to access these values outside startup.cs
class?
You can see my code here for detail. Basically I want to read email setting and the structure of my email setting look like this
Then I will need to define a class like this to hold all of information in appSetting
Finally I inject into my service class or whatever you want
then call
Email sender file can be found here
If you have any question just let me know.
** Note this example help you read appSetting inside service class like class library or we can access appsetting data from outside main mvc app.
You should register Configuration in ConfigureServices: