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
36 views
in .Net Framework by 21 52 67

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 21 52 67
 
Best answer

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

Simply by implement the below code:

  1. Add this in your constructor:

    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!
...