2017-03-16 12 views
6

Jestem zaznajomiony z załadowaniem sekcji appsettings.json do mocno wpisanego obiektu w .NET Core startup.cs. Na przykład:Jak załadować sekcję appsetting.json na słownik w .NET Core?

public class CustomSection 
{ 
    public int A {get;set;} 
    public int B {get;set;} 
} 

//In Startup.cs 
services.Configure<CustomSection>(Configuration.GetSection("CustomSection")); 

//Inject an IOptions instance 
public HomeController(IOptions<CustomSection> options) 
{ 
    var settings = options.Value; 
} 

Mam sekcję appsettings.json, której pary klucz/wartość będą się różnić liczbą i nazwą w czasie. Dlatego nie jest praktyczne stosowanie twardych nazw właściwości kodu w klasie, ponieważ nowe pary klucz/wartość wymagałyby zmiany kodu w klasie. Małą próbkę niektórych par klucz/wartość:

"MobileConfigInfo": { 
    "appointment-confirmed": "We've booked your appointment. See you soon!", 
    "appointments-book": "New Appointment", 
    "appointments-null": "We could not locate any upcoming appointments for you.", 
    "availability-null": "Sorry, there are no available times on this date. Please try another." 
} 

Czy istnieje sposób, aby załadować te dane do obiektu MobileConfigInfo Dictionary, a następnie użyć wzoru nazwami opcji wstrzyknąć MobileConfigInfo do kontrolera?

+0

Hmmm isnt to pytanie naprawdę o ASP.NET Core, a nie .NET Core? Trochę mylącego tytułu. – nashwan

Odpowiedz

9

Można użyć Configuration.Bind(settings); w startup.cs klasy

I klasa ustawień będzie jak

public class AppSettings 
{ 
    public Dictionary<string, string> MobileConfigInfo 
    { 
     get; 
     set; 
    } 
} 

Nadzieję, że to pomaga!

3

Dla innych, którzy chcą przekształcić go w Słownik,

sekcja próbki wewnątrz appsettings.json

"MailSettings": { 
    "Server": "http://mail.mydomain.com"   
    "Port": "25", 
    "From": "[email protected]" 
} 

Poniższy kod należy umieścić w pliku startowego> Metoda ConfigureServices:

public static Dictionary<string, object> MailSettings { get; private set; } 

public void ConfigureServices(IServiceCollection services) 
{ 
    //ConfigureServices code...... 

    MailSettings = 
     Configuration.GetSection("MailSettings").GetChildren() 
     .Select(item => new KeyValuePair<string, string>(item.Key, item.Value)) 
     .ToDictionary(x => x.Key, x => x.Value); 
} 

Teraz możesz uzyskać dostęp do słownika f rom gdziekolwiek podoba:

string mailServer = Startup.MailSettings["Server"]; 

Jednym minusem jest to, że wszystkie wartości zostaną pobrane jako ciągi, jeśli spróbujesz innego rodzaju wartość będzie zerowa.

3

Go z tego formatu struktury:

"MobileConfigInfo": { 
    "Values": { 
     "appointment-confirmed": "We've booked your appointment. See you soon!", 
     "appointments-book": "New Appointment", 
     "appointments-null": "We could not locate any upcoming appointments for you.", 
     "availability-null": "Sorry, there are no available times on this date. Please try another." 
} 
} 

Dodać ustawienie klasa wyglądać następująco:

public class CustomSection 
{ 
    public Dictionary<string, string> Values {get;set;} 
} 

następnie zrobić to

services.Configure<CustomSection>((settings) => 
{ 
    Configuration.GetSection("MobileConfigInfo").Bind(settings); 
}); 
Powiązane problemy