Kể từ khi không có lớp ConfigurationManager trong NET Core, bây giờ tôi phải thiết lập cấu hình trong appsettings.json thay web.configKhông thể thiết lập cấu hình từ file JSON appSettings trong dự án .NET Lõi
Theo this bài đăng blog tôi có để đặt cấu hình của tôi ở đó, vì vậy tôi đã làm như thế này:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Conexion": {
"name" : "empresas",
"connectionString": "Data Source=empresas;Initial Catalog=CATALMA; Integrated Security=True;",
"providerName": "System.Data.SqlClient"
}
}
Tôi vừa mới ghi rằng "Conexion".
Bây giờ tôi tạo ra trong một thư mục ViewModels lớp sau:
public class ConexionConfig
{
public string name { get; set; }
public string connectionString { get; set; }
public string providerName { get; set; }
}
Bây giờ, Trong Startup.cs, trong phương pháp ConfigureServices, tôi phải bổ sung nó bằng cách:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.Configure<ConexionConfig>(Configuration.GetSection("Conexion"));
services.AddMvc();
}
Nhưng tiếc , Tôi nhận được lỗi sau:
Argument 2: cannot convert from
'Microsoft.Extensions.Configuration.IConfigurationSection' to
'System.Action<ConexionConfig>'
Tôi đang thiếu gì?
Xin chào, bạn đã bao giờ tìm được giải pháp cho điều này chưa? Tôi đã có chính xác cùng một vấn đề. – AndyM
Có, nhưng tôi thay đổi nội dung appsettings.json để khớp với một ví dụ mà tôi thấy ở một nơi khác. –
Xem câu trả lời được đăng –