Những lợi ích của việc sử dụng Options Pattern trong ASP.NET Core là gì?
Những lợi ích của việc sử dụng Options Pattern trong ASP.NET Core là gì?
Options pattern dùng lớp có kiểu mạnh (POCO) để đọc các nhóm cấu hình liên quan, thay vì đọc IConfiguration["A:B:C"] rải rác.
// appsettings.json
{ "Smtp": { "Host": "smtp.example.com", "Port": 587 } }
public class SmtpOptions
{
public const string Section = "Smtp";
[Required] public string Host { get; set; } = "";
[Range(1, 65535)] public int Port { get; set; }
}
builder.Services.AddOptions<SmtpOptions>()
.Bind(builder.Configuration.GetSection(SmtpOptions.Section))
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.