2017-03-16 18 views
6

Tôi quen thuộc với việc tải một phần appsettings.json vào một đối tượng được nhập mạnh mẽ trong .NET Core startup.cs. Ví dụ:Làm thế nào để tải phần appsetting.json vào từ điển trong .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; 
} 

Tôi có phần appsettings.json là cặp khóa/giá trị sẽ thay đổi về số lượng và tên theo thời gian. Do đó, nó không thực tế đối với các tên thuộc tính mã cứng trong một lớp vì cặp khóa/giá trị mới sẽ yêu cầu thay đổi mã trong lớp. Một mẫu nhỏ của một số cặp khóa/giá trị:

"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." 
} 

Có cách nào để tải dữ liệu này vào một đối tượng MobileConfigInfo điển và sau đó sử dụng mô hình IOptions để tiêm MobileConfigInfo vào một bộ điều khiển?

+0

Hmmm isnt câu hỏi này thực sự về ASP.NET Core và không .NET Core? Bit của một tiêu đề gây hiểu lầm. – nashwan

Trả lời

9

Bạn có thể sử dụng Configuration.Bind(settings); trong startup.cs lớp

Và lớp thiết lập của bạn sẽ như thế nào

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

Hy vọng nó sẽ giúp!

3

Đối với những người muốn chuyển đổi nó vào một từ điển ,

phần mẫu bên appsettings.json

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

Tiếp theo mã nên được đặt bên trong các tập tin Startup> ConfigureServices phương pháp:

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); 
} 

Bây giờ bạn có thể truy cập từ điển f rom ở bất cứ nơi nào như:

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

Một nhược điểm là tất cả giá trị sẽ được truy xuất dưới dạng chuỗi, nếu bạn thử bất kỳ loại giá trị nào khác sẽ là rỗng.

3

Go với định dạng cấu trúc này:

"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." 
} 
} 

Hãy thiết lập lớp nhìn bạn như thế này:

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

sau đó làm điều này

services.Configure<CustomSection>((settings) => 
{ 
    Configuration.GetSection("MobileConfigInfo").Bind(settings); 
}); 
Các vấn đề liên quan