Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
693 views
in .Net Framework by 63 69 85

Trying to get value from appsettings.json note that in the Project there is no Startup.cs file and there is no Program.Main().

I google it but there is a problematic solution like creating a new Model, etc...

Is there a simple way to get the ApiKey value from appsettings.json in  .NET Core 6.0?

{
  "ApiKey": "myApiKey",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

 


1 Answer

1 like 0 dislike
by 63 69 85
edited by
 
Best answer

How to Get Value from appsettings.json in .NET Core?

Simply by implement the below code:

  1. Inject Configuration in your constructor:

    private readonly IConfiguration _config;
    public MyController(IConfiguration config)
    {
        _config = config;
    }
    
  2. Get your Value using _config:

    string Key = _config.GetValue<string>("ApiKey");
    
If you don’t ask, the answer is always NO!
...