Hi,
I'm trying to bind a json file to classes in my ASP.NET core project and I only get null in my object.
The reading from the file is OK since I can't see the values in the config object, but it doesn't bind with my class.
Where's the problem?
So here's the json file:
{"Center": {"Location": [ {"Name": "GLOBULE de Brossard - Quartier Dix30","Adress": "9370, boulevard Leduc, local 30","City": "Brossard","Prov": "Québec","CP": "J4Y 0B3","Lundi": "10 h à 18 h","Mardi": "10 h à 18 h","Mercredi": "10 h à 18 h","Jeudi": "10 h à 20 h","Vendredi": "10 h à 20 h","Samedi": "9 h à 17 h","Dimanche": "10 h à 17 h","Lat": "45.4440401","Long": "-73.43603089999999" }, {"Name": "GLOBULE de Laval - Centre Laval","Adress": "1600, boulevard Le Corbusier (entrée 4)","City": "Laval","Prov": "Québec","CP": "H7S 1Y9","Lundi": "10 h à 18 h","Mardi": "10 h à 18 h","Mercredi": "10 h à 18 h","Jeudi": "10 h à 20 h","Vendredi": "10 h à 20 h","Samedi": "9 h à 17 h","Dimanche": "10 h à 17 h","Lat": "45.5603711","Long": "-73.7299956" } ] } }
The class:
public class CenterLocations { public Center Location { get; set; } } public class Center { public Location[] Locations { get; set; } } public class Location { string Name { get; set; } string Adress { get; set; } string City { get; set; } string Prov { get; set; } string CP { get; set; } string Lundi { get; set; } string Mardi { get; set; } string Mercredi { get; set; } string Jeudi { get; set; } string Vendredi { get; set; } string Samedi { get; set; } string Dimanche { get; set; } string Lat { get; set; } string Long { get; set; } }
The startup.cs
var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("CenterLocations.json") .Build(); services.AddOptions(); services.Configure<CenterLocations>(config); services.AddSingleton(config);
And the object is null in the controller:
public class MapController : Controller { private readonly CenterLocations _centers; public MapController(IOptions<CenterLocations> options) { _centers = options.Value; //Locations = null } public IActionResult Index() { return View(); } }