-1

I have the following appsettings.json inside my Asp.NET core console application:-

{
  "ConnectionStrings": {
    "ConnectionString": "Server=localhost;Database=ServiceDesk;Trusted_Connection=True"
  },
  "SP": {
    "SiteURL": "https://***.sharepoint.com/"
  }
}

now i want to access the SiteURL, i tired the following but it did not work:-

var section = config.GetSection("SP");
var ClientConfig = section.GetChildren();
string siteUrl = ClientConfig.ToString();
PWND
  • 409
  • 3
  • 11
John John
  • 1
  • 72
  • 238
  • 501

2 Answers2

0

Create a model that matches the settings to store the data

public class SharePointOptions {
    public string SiteURL { get; set; }
}

Then bind to the section from configuration

SharePointOptions options = config.GetSection("SP").Get<SharePointOptions>();
string siteUrl = options.SiteURL;

Reference Configuration in ASP.NET Core

ConfigurationBinder.Get<T> binds and returns the specified type. ConfigurationBinder.Get<T> may be more convenient than using ConfigurationBinder.Bind.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
-1

You can get value from configuration by key like this.

appsettings.json

{
    "key": "value"
}
var v = config["key"];

for nested values use Parent:Key notation

Filip Kováč
  • 499
  • 8
  • 15