Here is my code:
public class YouTubeShowsServices
{
private IConfiguration _config;
string YouTubeApiKey;
string YoutubeAppName;
string YoutubePlaylistId;
public YouTubeShowsServices(IConfiguration configuration)
{
_config = configuration;
YouTubeApiKey =_config["YouTube:Key"];
YoutubeAppName = _config["YouTube:AppName"];
YoutubePlaylistId = _config["YouTube:PlayListId"];
}
The variables YouTubeApiKey, YouTubeAppName and YoutubePlaylistId are getting set to null.
Here is my appsettings.json:
"YouTube":
{
"Key": "AIzaSyDmGZCNbxkhmS5FW-FEhzK94z0J1WWVavE",
"PlayListId": "PLwW36SAh95SUivPyG1eUxLM8v4rjwnEfT",
"AppName": "LiveStandup"
}
I am trying to read the configuration from appsettings.json, but my variables end up being null - can anyone help me with this code?
Add NuGet Package:
To include the required NuGet package, use the following command:
Consolidate Controller Configuration:
Consolidate the desired controller configuration under a single category, such as "EmailSettings", in the
appsettings.jsonfile:Step 3:
Create a class named
EmailSettings.cswithin your Web API project:Step 4:
Associate your configuration values with an instance of the
EmailSettingsclass inStartup.cs, and inject this object into the Dependency Injection container:In
Startup.cs:Step 5:
You can now easily include your configuration in the constructor of the API controller to make a request:
Example controller for Web API:
Now, your
ValuesControllerhas access to theEmailSettingsconfigured in yourappsettings.jsonfile.