Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read appsettings.json - Fields remain null

Think I have a problem with the startup.cs as I do not get any values from my <IOption> config

So.. We have our appsettings.json

"Config": {
    "ApplicationName": "some name",
    "ConnectionString": "someconstring",
    "Version": "1.0.0"
  },

Here we have our model

public class Config
    {   
        public string ApplicationName { get; set; }
        public string ConnectionString { get; set; }
        public string Version { get; set; }
    }

The startup.cs

 public Startup(IConfiguration configuration)

    {
        Configuration = configuration;
    }



public static IConfiguration Configuration { get; set; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        // Add functionality to inject IOptions<T>
        services.AddOptions();

        // Add our Config object so it can be injected
        services.Configure<Config>(Configuration);
    }

And then in our controller I try to load those data but unfortunately they remain empty.

   private IOptions<Config> config;
            public CompaniesController(IOptions<Config> config)
            {
                this.config = config;
            }

I've tried to change the startup.cs with something like

 services.Configure<Config>(options =>
            {
                options.ConnectionString = Configuration.GetSection("Config:ConnectionString").Value;
            });

but that doesn't seems to work.

Resources I've been using:

https://dzone.com/articles/dynamic-connection-string-in-net-core

https://stackoverflow.com/questions/31453495/how-to-read-appsettings-values-from-json-file-in-asp-net-core

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.2

but Obviously I am missing a crucial point here.

edit: I am using ASP.Net Core 2.0

enter image description here

edit2:

the Program.cs

public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }

The entire Appsettings.json file

{
  "Config": {
    "ApplicationName": "somename",
    "ConnectionString": "someconstring",
    "Version": "1.0.0"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

edit 3: In my front-end application I import the API like this. enter image description here

like image 913
Falcon Avatar asked Jan 09 '19 16:01

Falcon


1 Answers

services.Configure<Config>(Configuration);

This line doesn't achieve the desired result because the JSON properties you're looking for are nested under a Config property in your appsettings.json file. To load these values as intended, use GetSection to grab the Config section and pass that into the Configure<TOptions> method:

services.Configure<Config>(Configuration.GetSection("Config"));
like image 116
Kirk Larkin Avatar answered Nov 13 '22 20:11

Kirk Larkin